【问题标题】:Create thumbnails, fixed sizes - PHP创建缩略图,固定大小 - PHP
【发布时间】:2014-05-17 01:51:08
【问题描述】:

我想从图像创建缩略图,但尺寸固定 - 310 像素宽,217 像素高。 我的代码: list($width, $height) = getimagesize($filename); $width = 310; $height = 217; $new_width = 310; $new_height = 217; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);......

我怎样才能做到这一点?公式是什么?

【问题讨论】:

标签: php gd thumbnails


【解决方案1】:
function resizeImage($url, $width, $height, $url_out, $keep_ratio = false){
  if($height <= 0 && $width <= 0)
    return false;
  else{

copy($url, $url_out);
$info = getimagesize($url);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if($keep_ratio){
  if($width == 0)
    $factor = $height/$height_old;
  elseif($height == 0)
    $factor = $width/$width_old;
  else
    $factor = min($width / $width_old, $height / $height_old);
  $final_width = round( $width_old * $factor );
  $final_height = round( $height_old * $factor );
}
else{
  $final_width = ($width <= 0) ? $width_old : $width;
  $final_height = ($height <= 0) ? $height_old : $height;
}
switch($info[2]){
  case IMAGETYPE_GIF:
    $image = imagecreatefromgif($url_out);
    break;
  case IMAGETYPE_JPEG:
    $image = imagecreatefromjpeg($url_out);
    break;
  case IMAGETYPE_PNG:
    $image = imagecreatefrompng($url_out);
    break;
  default:
    return false;
}
$image_resized = imagecreatetruecolor($final_width, $final_height);
if($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG){
  $transparency = imagecolortransparent($image);
  if($transparency >= 0){
    $transparent_color = imagecolorsforindex($image, $trnprt_indx);
    $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
    imagefill($image_resized, 0, 0, $transparency);
    imagecolortransparent($image_resized, $transparency);
  }
  elseif($info[2] == IMAGETYPE_PNG){
    imagealphablending($image_resized, false);
    $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
    imagefill($image_resized, 0, 0, $color);
    imagesavealpha($image_resized, true);
  }
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
switch($info[2]){
  case IMAGETYPE_GIF:
    imagegif($image_resized, $url_out);
    break;
  case IMAGETYPE_JPEG:
    imagejpeg($image_resized, $url_out);
    break;
  case IMAGETYPE_PNG:
    imagepng($image_resized, $url_out);
    break;
  default:
    return false;
}
imagedestroy($image_resized);
return true;
  }
}

//只需调用该函数,并更改图像名称

$url="desert09.jpg";
resizeImage($url, 310, 217,"output.jpg", $keep_ratio = false);

【讨论】:

    【解决方案2】:

    我有这段代码(我不记得我从哪里得到的)并且我已经对其进行了一些编辑。我希望它可以帮助你,这会创建一个固定的正方形大小的拇指:

    /** Create a square cropped thumb **/
    function createSquareCroppedThumb($path , $thumbPath, $thumbSize = 100 ){
            global $max_width, $max_height;
    
        /* Set Filenames */
        $srcFile    = $path;
        $thumbFile  = $thumbPath;
    
        /* Determine the File Type */
        $type = pathinfo($path, PATHINFO_EXTENSION);
        /* Create the Source Image */
        switch( $type ){
            case 'jpg' : case 'jpeg' :
              $src = imagecreatefromjpeg( $srcFile ); break;
            case 'png' :
              $src = imagecreatefrompng( $srcFile ); break;
            case 'gif' :
              $src = imagecreatefromgif( $srcFile ); break;
        }
        /* Determine the Image Dimensions */
        $oldW = imagesx( $src );
        $oldH = imagesy( $src );
    
        $minValue = $oldH > $oldW ? $oldW : $oldH;  
    
        /* Create the New Image */
        $new = imagecreatetruecolor( $thumbSize , $thumbSize );
    
        /* Transcribe the Source Image into the New (Square) Image */
        imagecopyresampled($new , $src , 0 , 0 , ($oldW / 2) - ($minValue /2) , ($oldH / 2) - ($minValue /2) , $thumbSize , $thumbSize , $minValue , $minValue );
        switch( $type ){
            case 'jpg' : case 'jpeg' :
              $src = imagejpeg( $new , $thumbFile ); break;
            case 'png' :
              $src = imagepng( $new , $thumbFile ); break;
            case 'gif' :
              $src = imagegif( $new , $thumbFile ); break;
        }
    
      imagedestroy( $new );
    }
    

    【讨论】:

    • 这适用于 310x310,但如何使其适用于 310x217?
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2013-06-22
    • 2013-06-24
    • 2010-12-18
    • 2012-07-15
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多