Problem with resizing images in PHP...

Discussion in 'PHP' started by fatabbot, Nov 2, 2006.

  1. #1
    Hello,

    I'm trying to resize images in php to make thumbs... The resize goes ok, but the quality of the resized image is very bad, although i set the jpg quality to 100.
    I use this code to resize:

    
    ...
    //create medium thumb version
    $new_width = floor($mediumthumbscale*$imgwidth);
    $new_height = floor($mediumthumbscale*$imgheight);
    //Create a new temporary image
    $tmp_img = imagecreatetruecolor($new_width, $new_height);
    //Copy and resize old image into new image
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $imgwidth, $imgheight);
    imagejpeg($tmp_img, $imgthumbpath.$mediumimagefilename,100);
    ...
    
    PHP:

    So the sizes are all correct and the thumb file gets created, but the quality is much poorer than when i do the resize manually in photoshop for example.

    Does anyone have an idea what the problem is here ?
     
    fatabbot, Nov 2, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Some versions of GD don't support 16 million colours. Try upgrading.
     
    mad4, Nov 2, 2006 IP
  3. fatabbot

    fatabbot Well-Known Member

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    138
    #3
    I have the latest PHP and GD installed on the server.
    Any other reason why this might happen ?
     
    fatabbot, Nov 4, 2006 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Examples (images) would be nice..

    Peace,
     
    Barti1987, Nov 4, 2006 IP
  5. fatabbot

    fatabbot Well-Known Member

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    138
    #5
    Sure, i attached both results.
    The left one is the result with PHP, the right one is the result when i manually resize in photoshop or fireworks.
    The right one is much better and even 4 times smaller in size...
     

    Attached Files:

    fatabbot, Nov 5, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    phpThumb() might help.

    http:// phpthumb.sourceforge.net/
     
    nico_swd, Nov 5, 2006 IP
  7. RRWH

    RRWH Active Member

    Messages:
    821
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    70
    #7
    the problem is you are using the wrong function here - I suggest that you consider using imagecopyresampled() rather than imagecopyresized()

    A couple of snippets from the PHP online documentation should clarify this for you.

    imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

    imagecopyresized() copies a rectangular portion of one image to another image. dst_image is the destination image, src_image is the source image identifier. If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.

    You are currently stretching or shrinking the image and ending up with cr*p - if you resampled the image you would not have this problem.
     
    RRWH, Nov 5, 2006 IP
    fatabbot likes this.
  8. fatabbot

    fatabbot Well-Known Member

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    138
    #8

    AAaaah, damn you're right.
    Thanks alot for clearing this up.
     
    fatabbot, Nov 6, 2006 IP
  9. skylark

    skylark Guest

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I currently use phpThumb() and it works great for my needs on my site http://www.pointrelevance.com. I tried building a function from scratch but decided not to try to re-invent the wheel when my initial results were pretty crappy.
     
    skylark, Nov 8, 2006 IP
  10. fatabbot

    fatabbot Well-Known Member

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    138
    #10
    It's not that complicated to do it manually.
    What does php_thumb do ? Does it have any special functionalities ?
     
    fatabbot, Nov 11, 2006 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    nico_swd, Nov 11, 2006 IP