2017-02-24 5 views
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); 

答えて

0

あなたのコードは非常にうまく機能し、この

//resize and crop image by center 
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){ 
    $imgsize = getimagesize($source_file); 
    $width = $imgsize[0]; 
    $height = $imgsize[1]; 
    $mime = $imgsize['mime']; 

    switch($mime){ 
     case 'image/gif': 
      $image_create = "imagecreatefromgif"; 
      $image = "imagegif"; 
      break; 

     case 'image/png': 
      $image_create = "imagecreatefrompng"; 
      $image = "imagepng"; 
      $quality = 7; 
      break; 

     case 'image/jpeg': 
      $image_create = "imagecreatefromjpeg"; 
      $image = "imagejpeg"; 
      $quality = 80; 
      break; 

     default: 
      return false; 
      break; 
    } 

    $dst_img = imagecreatetruecolor($max_width, $max_height); 
    $src_img = $image_create($source_file); 

    $width_new = $height * $max_width/$max_height; 
    $height_new = $width * $max_height/$max_width; 
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa 
    if($width_new > $width){ 
     //cut point by height 
     $h_point = (($height - $height_new)/2); 
     //copy image 
     imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); 
    }else{ 
     //cut point by width 
     $w_point = (($width - $width_new)/2); 
     imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); 
    } 

    $image($dst_img, $dst_dir, $quality); 

    if($dst_img)imagedestroy($dst_img); 
    if($src_img)imagedestroy($src_img); 
} 
//usage example 
resize_crop_image(100, 100, "test.jpg", "test.jpg"); 

source

+0

をお試しください!しかし、私はあなたが私にどこを編集するかを教えてもらえますか? IFの幅が高さよりも大きい。私は高さを切り抜きたくない。 –

関連する問題