SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Resizing an image

    Hi, I have to resize an image to 100px in width when it's bigger than 100px in width. Also the height most resize auto along with width.
    I've got the following but it doesn't work

    PHP Code:

    $image_size 
    getimagesize($image_name);
              
    $width $image_size[0];
              
    $height $image_size[1];
              
              
    $fixed_size = while ($width 100) { $width };
              
              
    $height $height $fixed_size 
    I get a parse error btw...so I don't really know if the code would actually work?
    Any help plz
    thnx

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason that it doesn't work is that this:
    Code:
       $fixed_size = while ($width > 100) { $width - 1 };
    is meaningless PHP code - you can't assign a while statement to a variable as it doesn't return a value.

    What are you trying to achieve with this bit of code? Tell us wht it needs to do and we may be able to help.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the idea is to resize an image larger than 100pixels in width to 100 pixels, but the height has to change equaly too.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe calculate the ratio of the new width in respect to the original size and then dividy the height by that ration.

    PHP Code:
    $image_size getimagesize($image_name);
    $width $image_size[0];
    $height $image_size[1];

    if (
    $width 100)
    {
       
    $ratio $width 100;
       
    $width 100;
       
    $height /= $ratio;

    Maybe you need to use different variables but it should work.
    Last edited by Chris82; Mar 12, 2002 at 07:15.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get a parse error with that last code.

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not anymore I think, I missed the $ before image_size

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aah I checked your code a few times but I never find those little errors

    Anyway, I pasted it in my code but then I get a message that it can't open my image, while it works fine without the extra resize code

    PHP Code:
    $sql = @mysql_query ("SELECT * FROM news_international ORDER BY nid DESC LIMIT 1");  
              if (!
    $sql) { echo ("Can't request national news"); }
              
              echo (
    "<table width=\"230\" bgcolor=\"#E5E5E5\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\">\n");
              echo (
    "<tr bgcolor=\"#E5E5E5\">\n");
              echo (
    "<td colspan=\"2\">\nInternationaal Nieuws Headlines</td>\n");
              echo (
    "</tr>\n");
              echo (
    "<tr height=\"1\" bgcolor=\"#999999\">\n");
              echo (
    "<td colspan=\"2\">\n</td>\n");
              echo (
    "</tr>\n");
              
              while (
    $international_array mysql_fetch_array ($sql)) {
              
    $nid $international_array ["nid"];
              
    $title $international_array ["title"];
              
    $date $international_array ["date"];
              
    $message substr($international_array ["message"],0,200);
              
    $image_name $international_array ["image_name"];
              
              
    $image_size getimagesize($image_name);
              
    $width $image_size[0];
              
    $height $image_size[1];

              if (
    $width 100)
              {
              
    $ratio $width 100;
              
    $width 100;
              
    $height /= $ratio;
              }
              
              
              
              echo (
    "<tr bgcolor=\"#F2F2F2\">\n");
              echo (
    "<td width=\"130\">\n<b>$title</b></td>\n");
              echo (
    "<td width=\"100\" align=\"right\" valign=\"top\">\n$date</td>\n");
              echo (
    "</tr>\n");
              echo (
    "<tr bgcolor=\"#F2F2F2\">\n");
              echo (
    "<td colspan=\"2\">\n");
              echo (
    "<img src=\"admin/images_upload/$image_name\" align=\"left\" height=\"$height\" width=\"$width\">$message...&nbsp;<a href=\"news_international.php#$nid\" class=\"shortnews\">lees verder</a>");
              echo (
    "</td>\n");
              echo (
    "</tr>\n");
              
              }
              
              echo (
    "<tr bgcolor=\"#F2F2F2\">\n");
              echo (
    "<td colspan=\"2\">\n<div align=\"center\"><hr width=\"150\" noshade size=\"1\"></div></td>\n");
              echo (
    "</tr>\n");    
    echo (
    "</table>\n"); 
    Why do I get an error?

  8. #8
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by InfraRedesign
    Well the idea is to resize an image larger than 100pixels in width to 100 pixels, but the height has to change equaly too.
    Ooops sorry, my fault for not reading your first post properly

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could anyone tell me why it doesn't show my image with that extra code plz

  10. #10
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the error message you get?

    I tried it for a single image and it worked.

    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    $image_size getimagesize("ikb.jpg");

    print_r($image_size);

    $width $image_size[0];
    $height $image_size[1];

    if (
    $width 100)
    {
       
    $ratio $width 100;
       
    $width 100;
       
    $height /= $ratio;
    }

    echo 
    "Ratio = $ratio<br>";
    echo 
    "width = $width<br>";
    echo 
    "height = $height<br>";

    echo 
    "<img src='ikb.jpg' width='$width' height='$height'>";

    ?>
    Maybe there is something wrong with the mysql result? :/

  11. #11
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the following error with the resize code:

    Warning: getimagesize: Unable to open '020310_krantz.jpg' for reading. in /home/sites/url.... on line 104


    I would like to know why though
    The image displays fine without the resize code.

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I've found the prob

    so I get the error

    Warning: getimagesize: Unable to open '020310_krantz.jpg' for reading. in /home/sites/site150/web/infra/index7.php on line 104

    While the picture is at
    /home/sites/site150/web/infra/admin/images_upload/..

    How can I fix this?

  13. #13
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at my first post here:
    http://www.sitepointforums.com/showt...threadid=33888

    Sean
    Harry Potter

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

  14. #14
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried adding the full path..but it didn't work

  15. #15
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are calling getImageSize to early in your script.

    excerpt from your code after resizing:
    "<img src=\"admin/images_upload/$image_name\"

    I assume that this works properly?

    If so then change the getImageSize part to something along these lines:

    PHP Code:
    $image_size getimagesize("admin/images_upload/$image_name"); 
    Hopefully it will work then.

  16. #16
    SitePoint Member snuffles's Avatar
    Join Date
    Sep 2004
    Location
    singapore
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    resizing an image

    hi! i own a blog and i need to know how to resize an image an i need an EXAMPLE of the html.. plsplspls help mi.. look in my site to see the problem.. www.chocfreako.blogspot.com . thx alot!! and reply asap!

  17. #17
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how I do it:

    PHP Code:
         //resize image
         //$images_dir contains the path to your images directory
         //$filename -> duh...
         
    $size=GetImageSize($images_dir."/".$filename);
         
    //check if picture is in landscape or portrait
         
    if($size[0] > $size[1])
         {
          
    $width 350;
          
    $heigth = (int)(350 $size[1] / $size[0]);
         }
         else
         {
          
    $width = (int)(350 $size[0] / $size[1]);
          
    $heigth 350;
         }
         
    //create smaller image with GD 1.x.x
         //$filetype is jpg or something like that
         
    $function_suffix=$gd_function_suffix[$filetype];
         
    $function_to_read="ImageCreateFrom".$function_suffix;
         
    $function_to_write="Image".$function_suffix;
         
    //read source image
         
    $source_handle=$function_to_read($images_dir."/".$filename);
         if(
    $source_handle)
         {
          
    //create empty image
          
    $im=ImageCreate($width$heigth);
          
    //resize
          
    ImageCopyResized($im$source_handle0000$width$heigth$size[0], $size[1]);
         }
         
    //Save it
         
    $function_to_write($im$images_dir."/".$filename);
         
    ImageDestroy($im); 
    As you can see you need GD funtion library installed.
    make sure your directory where to save the image has writing permissions too

    hope this helps,

    greetz,
    koen

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $width getimagesize($path."/".$file);
    if(
    $width 100) {
    echo 
    "<img src='".$path."/".$file."' width='100'>";

    height is auto!
    cheers

  19. #19
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    PHP Code:
    $width getimagesize($path."/".$file);
    if(
    $width 100) {
    echo 
    "<img src='".$path."/".$file."' width='100'>";

    height is auto!
    cheers
    indeed for showing the image on the webpage right away that's tru.
    but what if you want to store the image? resizing it makes calling for the image later on not only easier (since the size is allready ok), you use lesser space on your webhosts server too (most important goal with resizing images)

    greetz,
    koen

  20. #20
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for resize image and store it u can use gd library or some javascript
    cheers

  21. #21
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    for resize image and store it u can use gd library or some javascript
    cheers
    I suggest you read my second previous post again a long with the code I submitted
    no offence

  22. #22
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i saw it now!
    yes it is just what i wanna say!
    but i saw a nice javascript function for the same thing!

  23. #23
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    i saw it now!
    yes it is just what i wanna say!
    but i saw a nice javascript function for the same thing!
    can you post it?
    not that I like using JavaScript very much but sometimes it comes in handy...

    thx in advance

  24. #24
    SitePoint Zealot tss68nl's Avatar
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bad bad practice to resize on the client:
    1) HTML resizing (what you do with both the php and javascript) will cause resize errors because of the louzy algorithm behind it
    2) The user will have to download a way bigger image than is actually displayed, thus wasting bandwidth.

    I'd say: GD library solution

  25. #25
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tss68nl
    bad bad practice to resize on the client:
    1) HTML resizing (what you do with both the php and javascript) will cause resize errors because of the louzy algorithm behind it
    2) The user will have to download a way bigger image than is actually displayed, thus wasting bandwidth.

    I'd say: GD library solution
    I use GD... (see previous post) but it might come in handy sometime?? you never know. that's why I asked the code...


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
  •