I'm trying to resize uploaded images so they fit into a slideshow.

Specifically, I'm interested in the height of the image as the slider doesn't handle different heights too well.

So I've implemented the code below:

<?php
function resizePhoto($ht,$fname,$dest,$pref,$SRCPath){
  $image_path=$SRCPath.$fname;
  $image_atts=getimagesize($image_path);
  if(!$image_atts[0]){
    echo 'Image data is corrupt, cannot process file:'.$fname;
    echo '<br>'.$image_path.'<br>';
    return '';
  }
  $wd=$image_atts[0]*($ht/$image_atts[1]);
  $img=imagecreatetruecolor($wd,$ht);
  $white=imagecolorallocate($img,255,255,255);
  $src=imagecreatefromjpeg($image_path);
  $parts=explode(".",$fname);
  $fname=$parts[0];
  for($i=1;$i<$parts.count;$i++){
    $fname.=".".$parts[$i];
    if($i<$parts.count-1){
      break;
    }
  }
  $fname.=".jpg";
  imagecopyresampled($img,$src,0,0,0,0,$wd,$ht,$image_atts[0],$image_atts[1]);
  $fname=$pref.$fname;
  imagejpeg($img,$dest.'/'.$fname);
  return $fname;
}

I call this using something like the following: resizePhoto(500,'IMG00012.JPG','gallery/resized/','tmp/')

I'm expecting the height to be 500 pixels, and I'm using $wd=$image_atts[0]*($ht/$image_atts[1]); to find out the appropriate width for the aspect ratio.

However, the width of the images are 500 pixels and it is the height which is varying.

For the life of me, I can't see where my code is going wrong.

Maybe it needs a fresh pair of eyes.

Hi, try to round the result of $wd because if for example this is a 4:3 it would return 666,67 i.e. a float, where instead imagecreatetruecolor()expects an integer.

Great!

I think that may be it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.