PHPのGDで画像のリサイズをしているのですが、リサイズ後の画像が縦に伸びたり横に伸びたりで
うまくいきません。
やりたいこととしましては、プロフィール写真は160200で、プライベート写真は250200でリサイズしたいです。
コードはネットで見つけたものを多少修正しています。
以下にコードを記述しますので、どなたかご教授いただければと思います。
よろしくお願いします。
↓呼び出し元
php
1$photo_dir = '../photo/'; 2$photo_dir1 = '../photo_resize/'; 3 4// プロフィール写真 5if (file_exists($photo_dir . $line['member_id'] . ".jpg")) { 6 if (file_exists($photo_dir1 . $line['member_id'] . ".jpg")) { 7 unlink($photo_dir1 . $line['member_id'] . ".jpg"); 8 } 9 $srcPath = $photo_dir . $line['member_id'] . ".jpg"; 10 $dstPath = $photo_dir1 . $line['member_id'] . ".jpg"; 11 makeThumbnail($srcPath, $dstPath, 160, 200); 12} 13 14// プライベート写真 15$photo2_dir = '../photo2/'; 16$photo2_dir1 = '../photo2_resize/'; 17if (file_exists($photo2_dir . $line['member_id'] . "_01.jpg")) { 18 if (file_exists($photo2_dir1 . $line['member_id'] . "_01.jpg")) { 19 unlink($photo2_dir1 . $line['member_id'] . "_01.jpg"); 20 } 21 $srcPath = $photo2_dir . $line['member_id'] . "_01.jpg"; 22 $dstPath = $photo2_dir1 . $line['member_id'] . "_01.jpg"; 23 makeThumbnail($srcPath, $dstPath, 250, 200); 24} 25if (file_exists($photo2_dir . $line['member_id'] . "_02.jpg")) { 26 if (file_exists($photo2_dir1 . $line['member_id'] . "_02.jpg")) { 27 unlink($photo2_dir1 . $line['member_id'] . "_02.jpg"); 28 } 29 $srcPath = $photo2_dir . $line['member_id'] . "_02.jpg"; 30 $dstPath = $photo2_dir1 . $line['member_id'] . "_02.jpg"; 31 makeThumbnail($srcPath, $dstPath, 250, 200); 32} 33if (file_exists($photo2_dir . $line['member_id'] . "_03.jpg")) { 34 if (file_exists($photo2_dir1 . $line['member_id'] . "_03.jpg")) { 35 unlink($photo2_dir1 . $line['member_id'] . "_03.jpg"); 36 } 37 $srcPath = $photo2_dir . $line['member_id'] . "_03.jpg"; 38 $dstPath = $photo2_dir1 . $line['member_id'] . "_03.jpg"; 39 makeThumbnail($srcPath, $dstPath, 250, 200); 40} 41
↓メソッド
php
1/* ---------------------------------------------------------- 2 * 画像のサイズを変形して保存する 3 * ---------------------------------------------------------- */ 4function transformImageSize($srcPath, $dstPath, $width, $height) 5{ 6 list($originalWidth, $originalHeight, $type) = getimagesize($srcPath); 7 switch ($type) { 8 case IMAGETYPE_JPEG: 9 $source = imagecreatefromjpeg($srcPath); 10 break; 11 case IMAGETYPE_PNG: 12 $source = imagecreatefrompng($srcPath); 13 break; 14 case IMAGETYPE_GIF: 15 $source = imagecreatefromgif($srcPath); 16 break; 17 default: 18 throw new RuntimeException("サポートしていない画像形式です: $type"); 19 } 20 21 $canvas = imagecreatetruecolor($width, $height); 22 23 imagecopyresampled($canvas, $source, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight); 24 imagejpeg($canvas, $dstPath, 100); 25 imagedestroy($source); 26 imagedestroy($canvas); 27} 28 29 30 /* ---------------------------------------------------------- 31 * 内接サイズを計算する 32 * ---------------------------------------------------------- */ 33 function getContainSize($width, $height, $containerWidth, $containerHeight) 34 { 35 if($containerWidth != 160){ 36 $ratio = $width / $height; 37 $containerRatio = $containerWidth / $containerHeight; 38 if ($ratio > $containerRatio) { 39 return array($containerWidth, intval($containerWidth / $ratio)); 40 } else { 41 return array(intval($containerHeight * $ratio), $containerHeight); 42 } 43 }else { 44 return array($containerWidth, $containerHeight); 45 } 46 //return array($containerWidth, $containerHeight); 47 48 } 49 50 /* ---------------------------------------------------------- 51 * 画像のサムネイルを保存する 52 * ---------------------------------------------------------- */ 53function makeThumbnail($srcPath, $dstPath, $maxWidth, $maxHeight) 54{ 55 list($originalWidth, $originalHeight) = getimagesize($srcPath); 56 if ($maxWidth > $originalWidth && $maxHeight > $originalHeight) { 57 copy($srcPath, $dstPath); 58 return; 59 } 60 61 list($canvasWidth, $canvasHeight) = getContainSize($originalWidth, $originalHeight, $maxWidth, $maxHeight); 62 63 transformImageSize($srcPath, $dstPath, $canvasWidth, $canvasHeight); 64} 65 66
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。