SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Grainy images using GD?

    Hi,

    I've got a script that uses GD to take a pdf and resize and resample it to a .jpg;

    PHP Code:
    <?php
    $result1
    =exec("convert test.pdf[0] test.jpg"); 
    $file="test.jpg"
    $size GetImageSize ("$file"); 
    $height=350
    $width=floor($size[0]/($size[1]/$height)); 
    $im ImageCreateFromJPEG ($file); 
    $im2ImageCreate ($width$height); 
    $width2=$size[0]; 
    $height2=$size[1]; 
    imagecopyresized ($im2$im0000$width$height$width2$height2); 
    $file3=substr("$file"0,-4)."_small.jpg"
    imagejpeg ($im2$file3100); 
    unlink ($file); 
    $newsize=GetImageSize ($file3);
    ?>
    Thing is though the images appear very grainy when they are converted. I've tried imagecopyresampled on this line:

    PHP Code:
    imagecopyresampled ($im2$im0000$width$height$width2$height2); 
    But that still makes the image appear grainy as well?

    Can anone help?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What does your image created directly after the PDF conversion look like? Is it suitable?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast Boxer's Avatar
    Join Date
    Jan 2008
    Location
    Denver, Colorado
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think the graininess is connected to PDF. I resampled regular JPG images and the thumnails were grainy, too. It's something with the GD library. It must have some configs we don't know about.

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I've found that imagecopyresampled produces much better images than imagecopyresized but you've tried that so i dont really know.

  5. #5
    SitePoint Enthusiast Boxer's Avatar
    Join Date
    Jan 2008
    Location
    Denver, Colorado
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GD documentation tells that GD resamples the big image only if the small image is created in TrueColor (using the function ImageCreateTrueColor).

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replace this line
    $im2= ImageCreate ($width, $height);
    with this:
    $im2= imagecreatetruecolor ($width, $height);

    This is especially important when original image is jpeg since jpegs are almost always truecolor images.


    And use imagecopyresampled()
    This will (most likely) improve the results and solve the problem with grainy images

  7. #7
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked a treat - thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •