SoulieBaby
SoulieBaby

Reputation: 5471

help needed to resize an image in php

I have a bit of code that generates thumbnails from an uploaded image. The only problem is that it cuts portrait images off, rather than resizing the portrait image to fit the height of the thumbnail, landscape images are fine. I was wondering if anyone could help me out to change the code so it will place portrait images inside the thumbnail properly? (if that makes sense?)

Here's the code:

$setSize        = 150;
$setHSize       = 113;
$jpeg_quality   = 75;

list($width, $height, $type, $attr) = getimagesize($origPath);

$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );

$img_r = imagecreatefromjpeg($origPath) or notfound();
$dst_r = ImageCreateTrueColor( $setSize, $setHSize );
$heightOffset = round( ($setHSize-$newH)/2 );

$white = imagecolorallocate($dst_r, 255, 255, 255);
imagefilledrectangle($dst_r, 0, 0, $setSize, $setHSize, $white);
imagecopyresampled($dst_r, $img_r, 0, $heightOffset, 0, 0, $newW, $newH, $width, $height);

header("Content-type: image/jpeg");
imagejpeg($dst_r, $thbPath, $jpeg_quality);

I just don't fully understand the way php creates images, so any help would be appreciated :)

Upvotes: 0

Views: 160

Answers (3)

AndyHua
AndyHua

Reputation: 1

adaptiveResizeImage

or

adaptiveCropThumblanil

in Imagick can help you

Upvotes: 0

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

The way you compute $newW and $newH is incorrect for what you want. You are giving preference to the width, so most landscape images will look okay, but portrait will be cut off. Try the following instead:

// assume landscape and see how things look
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
if ($newH > $setHSize) {
    // portrait image
    $newH = $setHSize;
    $newW = round( $width * ( $setHSize / $height ) );
}

I hope this helps!

Upvotes: 3

MHowey
MHowey

Reputation: 681

Hope this helps.

<?php 
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);  
include_once(DOCROOT."/dbc.php");

//**********************| Resize based on height

$photo_height = 350;

//**********************| Get the file from the post

$file = $_FILES['file'];
$path_thumbs = (DOCROOT."/gallery/"); 
$path_big = (DOCROOT."/trash/"); 

//**********************| Check permission

if (!is_writeable($path_thumbs)){
   die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
}
if (!is_writeable($path_big)){
    die ("Error: The directory <b>($path_big)</b> is NOT writable");
}

//**********************| Make sure you have a file

 if (isset($file)){

   $file_type = $_FILES['file']['type'];
   $file_name = $_FILES['file']['name'];
   $file_size = $_FILES['file']['size'];
   $file_tmp = $_FILES['file']['tmp_name'];

   $getExt = explode ('.', $file_name);
   $file_ext = $getExt[count($getExt)-1];

//**********************| Make new name

   $rand_name = md5(time());
   $rand_name= rand(0,999999999);

//**********************| See the kind of file we have

if($file_size){
  if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
  $new_img = imagecreatefromjpeg($file_tmp);
  }
  elseif($file_type == "image/x-png" || $file_type == "image/png"){
  $new_img = imagecreatefrompng($file_tmp);
  }
  elseif($file_type == "image/gif"){
  $new_img = imagecreatefromgif($file_tmp);
  }

//**********************| Get the height and width

  list($width, $height) = getimagesize($file_tmp);
       $imgratio=$height/$width;

 if ($photo_height >= $height){
 //*********** Dont resize if the image is smaller then $photo_height = 350;
 $newwidth = $width;
 $newheight = $height;
 }
 elseif ($imgratio>1){
 $newheight = $photo_height;
 $newwidth = $photo_height/$imgratio;
 }
 else{
 $newwidth = $photo_height;
 $newheight = $photo_height*$imgratio;
 }

 if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }
else{ die("Error: Please make sure you have GD library ver 2+");
}

imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext",'100');
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
$newimage = "$rand_name.$file_ext";
}
else { 
echo "Sorry, there was a problem, Please try again.\n\n";
}

Upvotes: 1

Related Questions