2017-11-07 17 views
1

このコードを実行すると、フォーム上の表のセル内にサムネイルを表示すると仮定すると、ブラウザの画面が黒く白く灰色の白い四角で囲まれます。サムネイル機能が正常に動作していません

function createThumb($imageUrl, $thumbWidth) 
{ 

// load image and get image size 
$img = imagecreatefromjpeg($imageUrl); 
$width = imagesx($img); 
$height = imagesy($img); 

// calculate thumbnail size 
$new_width = $thumbWidth; 
$new_height = floor($height * ($thumbWidth/$width)); 

// create a new temporary image 
$tmp_img = imagecreatetruecolor($new_width, $new_height); 

// copy and resize old image into new image 
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// display thumbnail 
header("Content-type:image/jpeg"); 
imagejpeg($tmp_img,$imageUrl,80); 
}; 

とコードを呼び出す行は、私は通常の構文エラーのいずれかを発見していない、とリードするように文法チェッカーを駆け抜けた時にコードがさえ戻ってきれいくる

<td > 
<?php echo createThumb("CoffeeReg.jpg",75)?> 
</td> 

です私のロジックの問題を信じるために、私は組み込みの関数を間違って使っています。あるいは、私は新しいサムネイルを表示するために何かを混乱させています。間違えた。私はこのミスから学ぶために、私が間違っていたことを知る必要があります。

+0

あなたの 'header'関数は、[documentation](http://php.net/manual/en/function.header.php)*のようにはうまくいかないと思います。実際の出力が通常のHTMLタグ、ファイル内の空白行、またはPHPから送られる前に呼び出されます。 '​​'タグの2番目のコードブロックは実際の出力です。あなたの 'createThumb'も何も返さずにエコーアウトします。 ['createThumb'](http://php.net/manual/en/function.imagejpeg.php)の第2引数は*"パスかオープンストリームリソースです...設定されていない場合、またはNULLの場合、生のイメージストリームは直接出力されます。 "* –

答えて

0

マジッドが私のコードでどこが間違っていたかを理解するのを助けるために提供してくれたコードを数時間デバッグした後、彼が作業を使用していたコードを作るために必要だったのは、二次のPHPファイルの末尾に2行のコード、ここで示したように...

<?php 
$imageUrl = $_GET['file_name']; 
$thumbWidth = $_GET['thumb_width']; 

// load image and get image size 
$mainImage = imagecreatefromjpeg($imageUrl); 
$mainwidth = imagesx($mainImage); 
$mainheight = imagesy($mainImage); 

// calculate thumbnail size 
$thumbWidth = intval($mainwidth/4); 
$thumbHeight = intval($mainheight/4);; 

// create a new temporary image 
$tmp_img = imagecreatetruecolor($thumbWidth, $thumbHeight); 

// copy and resize old image into new image 
imagecopyresampled($tmp_img, $mainImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainwidth, $mainheight); 

// display thumbnail 
header("Content-type:image/jpeg"); 

//Also had to remove the second and third argument from this function 
imagejpeg($tmp_img); 



//these following two lines here is what was missing 
imagedestroy(tmp_img); 
imagedestroy(mainImage); 
?> 

は、だから私は、1つのサムネイルコードの画像変数をクリアしなければならないことを推測するか、それが実行されるときには、それを破るだろう。なぜimagejpeg関数から2つの引数を取り除く必要があるのか​​分かりませんが、私の教科書で言及されているので、私はそれを試しました。ありがとうございました。皆さんの助けを捧げてありがとうございました。あなたの投稿がまだここに残っていれば、あなたは最高の投票と最善の答えで完全な信用を得ています。

関連する問題