SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image resize with GD???

    Yeah hello again, i was wandering of that where i resently installed the GD mod for PHP and the phpinfo() says itīs enabled but iīve used several scripts that i downloaded on several script pages and nothing works, iīve used the snipe.org gallery script that create thumbīs for all the uplodaded pics but nothing works, and the upload dir just turn the uplodaded pic were she should be putted ...but only the size of the image file is 0 byte :/

    Can i take some test on my server to check out wheiter my GD mod is working or not and the problem is something else?

    And does someone know of some little script that re-sizes pics using the php_GD mod ....

    Yebb the folder is 777 if you were gonna suggest that

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some code to test if GD is working or not:
    PHP Code:
    // create the image 
    $gif ImageCreate(200,200); 
    $bg ImageColorAllocate($gif,0,0,0); 
    $tx ImageColorAllocate($gif,255,128,128); 
    ImageFilledRectangle($gif,0,0,200,200,$bg); 
    ImageString($gif,3,70,90,"it works !",$tx); 

    // send the image 
    header("content-type: image/gif"); 
    ImageGif($gif); 
    I found that in the user comments of the Images page of the PHP manual:

    http://www.php.net/manual/en/ref.image.php

    Hope that helps,

    Skunk

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should help with the resizing question, this is for JPEGs:

    PHP Code:
    <?php

    if (!$max_width)
      
    $max_width 80;
    if (!
    $max_height)
      
    $max_height 60;

    $size GetImageSize($image);
    $width $size[0];
    $height $size[1];

    $x_ratio $max_width $width;
    $y_ratio $max_height $height;

    if ( (
    $width <= $max_width) && ($height <= $max_height) ) {
      
    $tn_width $width;
      
    $tn_height $height;
    }
    else if ((
    $x_ratio $height) < $max_height) {
      
    $tn_height ceil($x_ratio $height);
      
    $tn_width $max_width;
    }
    else {
      
    $tn_width ceil($y_ratio $width);
      
    $tn_height $max_height;
    }

    $src ImageCreateFromJpeg($image);
    $dst ImageCreate($tn_width,$tn_height);
    ImageCopyResized($dst$src0000,
        
    $tn_width,$tn_height,$width,$height);
    header("Content-type: image/jpeg");
    ImageJpeg($dstnull, -1);
    ImageDestroy($src);
    ImageDestroy($dst);

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sean,

    I have created a photo gallery of sorts for my site using PHP and MySQL. Currently, I upload two files, one being the main image and the other the thumbnail. I'd like to automate the process. Having considered the alternatives, I've decided I don't want to hold two images for each picture, so I'm trying to create thumbnails "on the fly" and send them to the browser, but I'm not quite sure how to use your code snippet.

    Two questions...

    1) Does the $image variable contain the name of the JPEG file, or the actual image itself?

    2) How would you call the page containing this script? Am I correct in assuming that it would be the object of an IMG tag in a parent page?

    Sorry if my questions are a tad on the basic side, but I'm still trying to get my head around all this PHP stuff!
    Paul Simpson, BSc, MCNI, MCNE

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put all of that code in a script called something like image.php. Then when you want a thumbnail just call it like this:

    <img src="image.php?image=seanspic">

    Then you can adjust the script to get the image name from get by putting $image = $HTTP_GET_VARS["image"]; at the top, just after the opening PHP tag. You will need to change the script so that it can only open images in a specified directory, this could be by simply putting the directory path before $image. You can then do other things too, like setting the max width and height in the query string

    Does that help?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed it does, thank you!

    So, to clarify, with the functions you've used $image is a text string containing the filename of the image (with path) and NOT the actual image itself.

    Wonderful, I'll get on to re-coding that part of my site straight away!
    Paul Simpson, BSc, MCNI, MCNE

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $image will contain the name of the image, for example if you call it with script.php?image=mypic then $image would be mypic. This makes the script safer as to actually do something to the image you will need to have to do something like this at the top of your script:

    PHP Code:
    $image "/path/to/image/$image.jpg"
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again!
    Paul Simpson, BSc, MCNI, MCNE


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
  •