SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    imagecopyresampled issue

    I've made function that resizes a given image, but when I change imagecopyresized with imagecopyresampled I'm encountering some problems.
    In Firefox I get a download prompt for the php file I'm running. In IE it says I can't display the page.

    I use the folowing code:

    PHP Code:
    // create a new image-resource
    $thumb =  imagecreatetruecolor($new_width$new_height);
    $new_source imagecreatefromjpeg($source);

    // Creates the thumbnail
    imagecopyresampled($thumb$new_source0000$new_width$new_height$img_width$img_height);
    imagejpeg($thumb$target100); 
    If I change it back to imagecopyresized it works just fine. Anyone have any idea why this happens?
    Last edited by lix0r; Apr 17, 2007 at 10:26.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sending any content headers?

    Try
    Code:
    header("Content-type: image/jpeg");
    I have attached a script that I have written in the past which takes in a path, maximum height and maximum width (through GET inputs) and outputs the image which the correct headers (GIF and JPEG currently). Feel free to use it as reference or the whole thing if you want.

    (Sorry its a .txt file cos the forum wouldn't let me upload a php file - you might want to change the extension back)
    Attached Files Attached Files

  3. #3
    SitePoint Enthusiast Sven S.'s Avatar
    Join Date
    May 2005
    Location
    Hamburg, Germany
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi lix0r,

    I have tried this on my localhost:
    PHP Code:
    <?php
    header
    ("content-type: image/jpeg");
        
    $source "Landschaft.jpg";
        
        
    // create a new image-resource 
        
    $new_width 64;
        
    $new_height 64;
        
        
    $thumb =  imagecreatetruecolor($new_width$new_height); 
        
    $new_source imagecreatefromjpeg($source); 
        
    $srcsize getimagesize("Landschaft.jpg");
        
        
    // Creates the thumbnail 
        
    imagecopyresized($thumb$new_source0000$new_width$new_height$srcsize[0], $srcsize[1]); 
        
    $target "test.jpg";
        
    imagejpeg($thumb""100);
    ?>
    This works fine for me whether I use imagecopyresized or imagecopyresampled.
    Hope this helps somehow.

    Sven Schoene

  4. #4
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. I have tried with an image-header, but it didn't work. It shouldn't be a header related problem, since I'm saving the image to a file and not outputting it directly to the browser.

    I've also tried with a text/html header, wiht no luck.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the line:
    Code:
    $new_width, $new_height, $img_width, $img_height);
    That looks like it might screw things up quite a bit!

  6. #6
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a mistake I did when I coppied it over here from the script :S

  7. #7
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can the filesize be causing this?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this all the code for the function? If not can you post the whole thing?

    Also, have you tried the code Sven S. posted?

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,397
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Is it something simple like a different version of GD?

    It works OK for me and I have 2.0.28 compatible.

    Also it would be interesting to see where you are getting your variables.

    The thing I would do is get the code to work independent of everything else to ensure it works on its own using Sven S.'s code. Then worry about getting it to work in the rest of your code.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did think it might be GD too. imagejpeg() requires at least version 1.8. It might be worth checking out just to be sure.

  11. #11
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is the same as Sven's, I just have som extra calculations of the new width and height.

    Anyways, here is the whole function:
    PHP Code:
            function resize_image($resize_limit$source$target) {

                list(
    $img_width$img_height$img_type$attr) = getimagesize($source);
            
                if (
    $img_width <=  $img_height) {
                    
                    
    $new_height $resize_limit;
                    
                    
    // Calculate the thunmbnail sizes
                    
    $ratio = (int) $img_width / (int) $img_height;
                    
    $new_width ceil( (int) $resize_limit * (float) $ratio );
                    
                    
                }
                
                if (
    $img_height <=  $img_width) {
                    
                    
    $new_width $resize_limit;

                    
    // Calculate the thunmbnail sizes
                    
    $ratio = (int) $img_height / (int) $img_width;
                    
    $new_height ceil( (int) $resize_limit * (float) $ratio );
                    
                    
                }
                
                if (
    $img_width ==  $img_height) {
                    
                    
    // Sets the correct height and width
                    
    $new_width $resize_limit;
                    
    $new_height $resize_limit;
                    
                }
                
                
    // create a new image-resource
                
    $thumb =  imagecreatetruecolor($new_width$new_height);
                
    $new_source imagecreatefromjpeg($source);

                
    // Creates the thumbnail
                
    imagecopyresampled($thumb$new_source0000$new_width$new_height$img_width$img_height);
                
    imagejpeg($thumb$target100);
                

                
                


  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that exact code works for me. There are several more things to check though:
    • The file you are reading from is readable
    • The file/directory you are trying to write to is writable
    • You have a new enough version of GD
    • The file you are reading from is a JPEG file and not something else

  13. #13
    SitePoint Enthusiast lix0r's Avatar
    Join Date
    Feb 2006
    Location
    Norway
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The files and directories are readable, and I have the GD version that is bundled with php5. All files that are uploaded must be jpg's, so that shouldn't be the problem.

    If it works fine on your server, I guess it somthing wrong with mine. Thanks for the help everybody

  14. #14
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Turn on the error reporting in this particular script and see if there is an output. Try FireBug (or some similar) firefox exception that will tell you what are the headers like. I'm guessing the PHP is throwing a fatal error of some sort or the process (php) could be terminated by some Segmentation Fault. The latter would explain why a file download is being forced upon you -- apache doesn't handle the request like it should. Happened to me a few times.

    Now for a shameless plug: you could try WideImage, an easy to use image library (php5). With that, the resize code could look like this:
    PHP Code:
    $img wiImage::load('Landschaft.jpg');
    echo 
    $img->resize(6464)->asString('jpeg'80); 
    regards

  15. #15
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure if you already solved your image resizing issue, but for you and all other devs like you looking for a way to transform images on the fly, you might want to check the open-source project Asido: there are a lot of useful features (http://www.asido.info/about/features/), including various types of image resize, watermarking, image copy, cropping, etc. It can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here's the list of drivers: http://www.asido.info/about/drivers/


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
  •