Results 1 to 9 of 9

Thread: PHP Image Resize

  1. #1

    PHP Image Resize

    Hi,
    I need to scale down an image in PHP to 20% of its original size. I have found another thread that is very close to my code but I still cannot get the image to show up once I add in the height and width %. My code is as follows:

    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width=20% height=20%></td>"
    This does not work so I have done the following two modifications without success as well:

    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width='20%' height='20%'></td>"
    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width=\"20%\" height=\"20%\"></td>"
    not too sure what to do next as I have wasted a lot of time now trying to figure this out. If someone can help I would be thankful.

    Cheers
    Michael

  2. #2
    Quote Originally Posted by kaykills
    Hi,
    I need to scale down an image in PHP to 20% of its original size. I have found another thread that is very close to my code but I still cannot get the image to show up once I add in the height and width %. My code is as follows:

    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width=20% height=20%></td>"
    This does not work so I have done the following two modifications without success as well:

    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width='20%' height='20%'></td>"
    PHP Code:
    print "<td colspan=8><img src=../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg width=\"20%\" height=\"20%\"></td>"
    not too sure what to do next as I have wasted a lot of time now trying to figure this out. If someone can help I would be thankful.

    Cheers
    Michael
    when you give an image the wirdth of 20% it takes 20% of the available space on a page. You'll have to hardcode the size. There is a PHP function that can help.

    PHP Code:
    <?

    // set the path to the image
    $image "../images/roster-images/roster-action-images/".$a_row['fname']."-".$a_row['lname']."-1.jpg";

    // get the sizes of the image, see http://nl.php.net/manual/en/function.getimagesize.php for more info
    $size getimagesize($image); 

    // calculate the width
    $width round($size[0] * 0.2);

    //calculate the height
    $heightround($size[1] * 0.2);

    print 
    "<td colspan=\"8\"><img src=\"" $image "\" width=\"" $width "\" height=\"" $height "\" /></td>";

    ?>
    when you create html try to make it XHTML compliant, this way you can avoid a lot of problems in future browsers.

    good luck

  3. #3
    cant you beter use GDLyb for this????

  4. #4
    Quote Originally Posted by sWo0p
    cant you beter use GDLyb for this????
    It depends. It will increase you server load and the script gets more complex. You would still have to calculate the size. Best way to do it is to generate a thumbnail and store that thumbnail on your server. This way it won't increase server load and even save bandwidth. Managing the images is more complex though. You'd have to manage both the full size images and the thumbnails.
    If you're willing to put in the efford it's worth it, I guess.
    In the past I created a thumbnail creation/caching script... I'll see if I it's something for you.

  5. #5
    i toke my eye in the web but looks like GDlyb is not standart in all php versions.. let it be.. looks mutch to complex for me.. its more easy to cut thumbs with pshop faster to..

  6. #6
    Here's a quick and dirty class I wrote for generating thumbnails using the GD library. Mosts hosts have the GD library installed and this is definitely the faster method rather than trying to do everything manually.

    This will scale an existing image on a server down to fit within the maximum widths and height that you supply.

    I haven't used this class in a while, but I believe you may be able to modify it to fit within a percentage of size quite easily.
    PHP Code:
    class image {
        
        
    // <---------------- VARIABLES ------------------->
        /*
         * INFO RELATED VARIABLES
         */
        
    var $size;
               
        
    // <---------------- FUNCTIONS ------------------->
             
        /*
         * GET IMAGE INFORMATION FUNCTION
         * $image = fullpath and name of image file
         */
        
        
    function getInfo($image)
            {
            
    $image_size getImageSize($image);
            
    $imgInfo['width'] = $image_size[0];
            
    $imgInfo['height'] = $image_size[1];
            
    $imgInfo['bytes'] = filesize($image);
            return 
    $imgInfo;
            }
            
        function 
    resizeJpeg($image$newName$maxWidth$maxHeight$destDir$quality$overwriteExisting true)
            {
            
    $scale min($maxWidth/$this->size['width'], $maxHeight/$this->size['height']);
            
    $new_image imageCreateFromJPEG($image);
            
            if (
    $image == $newName && $overwriteExisting == true)
                {
                
    unlink($image);
                }
            
            
    // Check scale
            
    if ($scale 1)
                {
                
    // Find new dimensions
                
    $new_width floor($scale*$this->size['width']);
                
    $new_height floor($scale*$this->size['height']);
                
                
    // Create empty image
                
    $tmp_image imageCreateTrueColor($new_width$new_height);
                
                
    // Copy imaged into resized empty images
                
    imageCopyResized($tmp_image$new_image00, -4, -4$new_width$new_height$this->size['width'], $this->size['height']);
                
                
    // Copy jpeg to destination directory
                
    imagejpeg($tmp_image$destDir.$newName$quality);
                
                
    // Update size of
                
    $this->size $this->getInfo($destDir.$newName);
                
                
    // Free up memory
                
    imageDestroy($tmp_image);
                
    imageDestroy($new_image);
                }
            }
        
        } 

  7. #7
    WOW..... thanks for your help..... ironikart, your class looked daunting so I tried teiz77's solution. It works wonderfully. Much appreciated you guys take the time to help others that are learning.

    Cheers
    Michael
    Kaykills

  8. #8
    you're welcome... I like helping people with php, it might prevent them form switching to asp.net or some simular crap

  9. #9
    yay for php!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012