2012-01-06 4 views
1

GDライブラリを使用して、アップロードされた画像のサムネイル版を自動的に生成しています。適切なimage____()関数を呼び出して、元の形式と同じ形式で保存します。私のコードはJPEGとGIFでうまく動作しますが、PNGファイルをアップロードすると、サムネイルが無効になります。実際には33バイトしか含まれていません(これまでに試したソースPNGを使用しています)。この画像はブラウザに表示されず、プレビュー(MacOS上)で開くこともできません。PHPのimagepng()メソッドが無効な画像を保存します

は、私が一緒にimagecreatetruecolor()を使用しimagecopyresampled()のように、サムネイルを生成するには:以下

function _resizeImageToFit($resource, $size) 
{ 
    $sourceWidth = imagesx($resource); 
    $sourceHeight = imagesy($resource); 
    if($sourceWidth >= $sourceHeight) { 
     // landscape or square 
     $newHeight = 1.0*$size/$sourceWidth*$sourceHeight; 
     $newWidth = $size; 
    } 
    else { 
     // portrait 
     $newWidth = 1.0*$size/$sourceHeight*$sourceWidth; 
     $newHeight = $size; 
    } 
    $thmb = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($thmb, $resource, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight); 
    return $thmb; 
} 

は私のセットアップのバージョン情報である

(これは1.9.4 MAMPバージョンです)ここ

PHPバージョン5.3.2バンドル GD版(2.0.34対応)

は無効発生親指の一例ですネイル画像(PNG):

 
âPNG

IHDRdaØMì∞

+1

あなたは、この関数の戻り値で何をしますか?ファイルシステムに保存するか、直接提供しますか? – dakdad

+0

'$ saveSuccess = $ imagesave($ thumbResource、$ thumbPath、100);の後に' imagedestroy($ thumbResource);が続きます。ここでは画像タイプに基づいて画像関数呼び出しの変数を使用します。これがPNGの場合、呼び出しは 'imagepng()'になります。 $ saveSuccess(imagepngから返された値)は、画像が壊れていても、成功を示すTRUEに設定されています。 –

答えて

7

私のエラーが見つかりました。 imagejpeg()は0から100までの範囲をとり、imagegif()はそのようなパラメータをとりません。私は100の品質でPNGを保存しようとしていました。

これはRTMの素敵なケースです。あなたの応答に感謝します。

http://ca3.php.net/manual/en/function.imagepng.php

http://ca3.php.net/manual/en/function.imagejpeg.php

http://ca3.php.net/manual/en/function.imagegif.php

+0

それは答えです – Santanu

0

この機能を試してみてください。

/** 
* Crop new images using the source image 
* 
* @param string $source - Image source 
* @param string $destination - Image destination 
* @param integer $thumbW - Width for the new image 
* @param integer $thumbH - Height for the new image 
* @param string $imageType - Type of the image 
* 
* @return bool 
*/ 
function cropImage($source, $destination, $thumbW, $thumbH, $imageType) 
{ 
    list($width, $height, $type, $attr) = getimagesize($source); 
    $x = 0; 
    $y = 0; 
    if ($width*$thumbH>$height*$thumbW) { 
     $x = ceil(($width - $height*$thumbW/$thumbH)/2); 
     $width = $height*$thumbW/$thumbH; 
    } else { 
     $y = ceil(($height - $width*$thumbH/$thumbW)/2); 
     $height = $width*$thumbH/$thumbW; 
    } 

    $newImage = imagecreatetruecolor($thumbW, $thumbH) or die ('Can not use GD'); 

    /* 
    if ($extension=='jpg' || $extension=='jpeg') { 
     $image = imagecreatefromjpeg($source); 
    } else if ($extension=='gif') { 
     $image = imagecreatefromgif($source); 
    } else if ($extension=='png') { 
     $image = imagecreatefrompng($source); 
    } 
    */ 
    switch($imageType) { 
     case "image/gif": 
      $image = imagecreatefromgif($source); 
      break; 
     case "image/pjpeg": 
     case "image/jpeg": 
     case "image/jpg": 
      $image = imagecreatefromjpeg($source); 
      break; 
     case "image/png": 
     case "image/x-png": 
      $image = imagecreatefrompng($source); 
      break; 
    } 

    if ([email protected]($newImage, $image, 0, 0, $x, $y, $thumbW, $thumbH, $width, $height)) { 
     return false; 
    } else { 
     imagejpeg($newImage, $destination,100); 
     imagedestroy($image); 
     return true; 
    } 
} 
関連する問題