0
イメージを400 x 300pxにサイズ変更したいと考えています。画像が幅400ピクセルより大きい場合、高さを切り取る前に、まず画像のサイズを変更したいと考えています。Php img切り抜く前にサイズ変更
画像は遠隔地のウェブサイトから来ているので、縦長でも構いませんが、できるだけ切り抜き量を最小限に抑え、可能であれば切り取る前にサイズを変更するのが目的です。
私は次のコードを使用しています(私は正直言って自分自身を非常に混乱させています)。比率と数字では必ずしも良好ではありません。コードはいくつかのSO応答コードからのものです。
function makeThumb($imgsrc, $imgtarg, $imgtarg_d) {
$ext = exif_imagetype($imgsrc);
if ($ext == false) {
return;
}
//getting the image dimensions
list($width, $height) = getimagesize($imgsrc);
//saving the image into memory (for manipulation with GD Library)
switch($ext) {
case 1:
$myImage = imagecreatefromgif($imgsrc);
break;
case 2:
$myImage = imagecreatefromjpeg($imgsrc);
break;
case 3:
$myImage = imagecreatefrompng($imgsrc);
break;
}
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height)/2;
$smallestSide = $height;
if ($width >= 400) {
$thumbSizeWidth = 400;
$thumbSizeHeight = 300;
} else {
$thumbSizeWidth = $width;
$thumbSizeHeight = 300;
}
} else {
$x = 0;
$y = ($height - $width)/2;
$smallestSide = $width;
if ($height >= 300) {
$thumbSizeHeight = 300;
$thumbSizeWidth = 400;
} else {
$thumbSizeHeight = $height;
$thumbSizeWidth = 400;
}
}
$thumb = imagecreatetruecolor($thumbSizeWidth, $thumbSizeHeight);
/*RESIZE FIRST*/
imagecopyresampled($thumb, $myImage, 0, 0, 0, $y, $thumbSizeWidth, $thumbSizeHeight, $width, $height);
/*CROP*/
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSizeWidth, $thumbSizeHeight, $smallestSide, $smallestSide);
//final output
imagejpeg($thumb, $imgtarg_d . '/' . $imgtarg,80);
imagedestroy($thumb);
}
画像は常に(意図したように)中心から切り取られているが、画像の幅が400ピクセル以上である場合には、最初のサイズ変更はありません。
imagecopyresampled($thumb, $myImage, 0, 0, 0, $y, $thumbSizeWidth, $thumbSizeHeight, $width, $height);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSizeWidth, $thumbSizeHeight, $smallestSide, $smallestSide);
をお試しください!しかし、私はあなたが私にどこを編集するかを教えてもらえますか? IFの幅が高さよりも大きい。私は高さを切り抜きたくない。 –