[PHP] image resize problem

Discussion in 'Web Design & Coding' started by Glaanieboy, May 12, 2004.

  1. Glaanieboy

    Glaanieboy Moderator

    The Netherlands
    $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;
        $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);
    }elseif($height < $width){
    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?
  2. Geffy

    Geffy Moderator Folding Team

    United Kingdom
    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
    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
  3. j79zlr

    j79zlr Glaanies script monkey Political User

    oops, geffy posted that already, ignore/