[PHP] image resize problem

Glaanieboy

OSNN Veteran Addict
Joined
Mar 6, 2002
Messages
2,628
Code:
<?php
$file = $_GET['file'];
list($height,$width,$type,$attr) = getimagesize($file);
$file_info = getimagesize($file);

$new_height = $_GET['max_height'];
$new_width = $_GET['max_width'];

//print $type;
$mime = $file_info['mime'];
//print $mime;
if ($height > $width){
  header("Content-type: $mime");
  $height_calc = $height - $new_height;
  $width_calc = $width - $new_width;
  
  if ($height_calc > $width_calc){
    $calc = $new_height / $height;
    $new_width = $width * $calc;
  }else{
    $calc = $new_width / $width;
    $new_height = $height * $calc;
  }
  if ($type == 3){
    //here is the problem
    $old_img = imagecreatefrompng($file);
    $new_img = imagecreatetruecolor($new_height,$new_width);
 imagecopyresampled($new_img,$old_img,0,0,0,0,$new_width,$new_height,$width,$height);
    imagepng($new_img);
    imagedestroy($new_img);
    imagedestroy($old_img);
  }


}elseif($height < $width){

}else{
}

?>
Don't mind the messyness of the code, it's still a work in progress. Basically the problem resides in the "//Here is the problem" part. I have an image, that must be resized to a calculated new size. It does calculate the sizes correctly, but then I miss about 20% of the image, the right 20% part to be precise. Click here for an example (Click here for the original file).

Can someone look at the code and tell me what's wrong?
 

Geffy

OSNN Veteran Addict
Joined
Mar 18, 2002
Messages
7,805
array getimagesize ( string filename [, array imageinfo])

The getimagesize() function will determine the size of any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag.

Note: Support for JPC, JP2, JPX, JB2, XBM, and WBMP became available in PHP 4.3.2. Support for SWC as of PHP 4.3.0.

Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. These values correspond to the IMAGETYPE constants that were added in PHP 4.3. Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.

an Example from php.net is
Code:
<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>
so it looks like on your list() call you have the $height and the $width getting the wrong values
 

j79zlr

Glaanies script monkey
Political User
Joined
Feb 16, 2003
Messages
2,725
oops, geffy posted that already, ignore/
 

Members online

No members online now.

Latest profile posts

Electronic Punk wrote on Sazar's profile.
Where are you buddy? :(
Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?

Forum statistics

Threads
61,978
Messages
673,285
Members
89,064
Latest member
Plesuvius