How to use GD library in php to resize an image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Evans
    New Member
    • Nov 2010
    • 9

    How to use GD library in php to resize an image

    I am creating a website that searches for photos. I found that functions in GD library are capable of resizing images through pre-created functions. I have created a test script to read and resize an image. It theoretically opens and reads an image to a variable then uses the read image to resize the image and display it before closing the file. The code I wrote is:

    **before going on: I used header and specified the type but was told that this information was passed. When I checked the type it did return image/jpeg which is why this truly stumps me. What is returned by fopen and fread seems to be symbolic still.

    Code:
    <html>
    <body>
    <?php
    $percent=0.5;
    $filename="http://bytes.com/images/dtown.jpg";
    $fp=fopen($filename,"rb");
    $file=fread($fp,filesize($filename));
    list($width,$height)=getimagesize($file);
    $new_width=$width*$percent;
    $new_height=$height*$percent;
    $image_p=imagecreatetruecolor($new_width,$new_height);
    $image=imagecreatefromjpeg($file);
    imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
    imagejpeg($image_p,null,100);
    fclose($fp);
    ?>
    </body>
    </html>
    However, I continue to get
    Code:
    Warning: getimagesize(ÿØÿà) [function.getimagesize]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\dumountaineer.com\httpdocs\dbimages\test.php on line 8
    
    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\Inetpub\vhosts\dumountaineer.com\httpdocs\dbimages\test.php on line 11
    
    Warning: imagecreatefromjpeg(ÿØÿà) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\dumountaineer.com\httpdocs\dbimages\test.php on line 12
    
    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\Inetpub\vhosts\dumountaineer.com\httpdocs\dbimages\test.php on line 13
    
    Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\Inetpub\vhosts\dumountaineer.com\httpdocs\dbimages\test.php on line 14
    Apparently, it is reading the file in symbols still. I specified binary in the fopen function with "rb".

    Is there a way to make this work? I am using a Windows Parallels Plesk Server and the symbols don't look like binary.
    ***I tried without opening the file and just linking to it and the entire image was just symbols. The PHP manual recommends using fopen but the symbols keep appearing. I have used fread and readfile to try and open the image. All help is appreciated.
    Last edited by Scott Evans; Dec 3 '10, 06:37 AM. Reason: more information needed
  • Scott Evans
    New Member
    • Nov 2010
    • 9

    #2
    I ended up finding a workaround. I cannot use some of the functions in my account or access my .ini file. I was able to use list(width,heig ht) and get_image_size to come up with a solution.

    Comment

    Working...