Question

I want to create thumbnails from image, but fixed sizes - 310px width, 217px height. My code: 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);......

How can I make that? What is the formula?

Was it helpful?

Solution

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;
  }
}

//just call the function, and change the image name

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

OTHER TIPS

I have this code (i don't remember where i got it) and i have edited it a little. I hope it can help you, this create a fixed square size thumb:

/** 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 );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top