Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation resizing images using php.

    Hi all,

    what i am looking for is a script which sets a boundry so to speak for the maximum size of any image e.g 100x100
    i dont want the images to be exactly 100x100 but i want them to be resized so that they fit inside a 100x100 boundry.

    i have several images of all shapes and sizes and i have tried setting the height='100' which works for images which are vertical but for those where the width is bigger than the height the image is too wide when using height=100.

    is there a way to do this so that the image is scaled down to fit within the 100x100.

    cheers
    Luke

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You could use getimagesize() function to get the dimensions and then fund a scaling factor, say
    PHP Code:
    $sizegetimagesize($_FILES['Photo']['tmp_name']);
    $width  $size[0];    // width of the image
    height $size[1];  
            
    if(
    $size[0]>$size[1])
     
    $scale=$size[0]/100;
    else
     
    $scale=$size[1]/100;

                            
    $new_width=    $size[0]/$scale;
    $new_height=$size[1]/$scale;
                            
    $new_image imagecreatetruecolor($new_width$new_height); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi Abduraooft,

    this loks like what i need the problem i have at the moment is that the images are contained within a rss feed and are pulled out using,

    {$item->imageURL}//which gives me the address of the image

    so i have used this in your code,

    PHP Code:
    $sizegetimagesize($item->imageURL);
    $width  $size[0];    // width of the image
    $height $size[1];  
            
    if(
    $size[0]>$size[1])
     
    $scale=$size[0]/100;
    else
     
    $scale=$size[1]/100;

                            
    $new_width$size[0]/$scale;
    $new_height=$size[1]/$scale;
                            
    $new_image imagecreatetruecolor($new_width$new_height); 
    but i am getting a

    Warning: Division by zero in C:\wamp\www\affiliate\test.php on line 150

    Warning: Division by zero in C:\wamp\www\affiliate\test.php on line 151

    any ideas?

    cheers
    Luke

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Does your $item->imageURL contain the complete path? If not make it complete by adding the rest of it.
    Check Example #3 in http://php.net/getimagesize
    Last edited by abduraooft; 12-15-2008 at 02:36 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi mate,

    im still havig trouble with this i am getting a Fatal error: Maximum execution time of 30 seconds exceeded.

    here is my code:

    PHP Code:
            $feed_url "http://www.kernow-connect.com/affiliate/feeds/hmvtop10.xml";

    // Get XML 
    $xml=simplexml_load_file($feed_url);

    // How many items to display from the RSS Feed ...
    $count 1


    foreach (
    $xml->product as $item) { 

    $image = ($item->imageURL);//example of url=http://www3.hmv.co.uk/hmv/Large_Images/HMVVIDEO/D012198.JPG
    $sizegetimagesize ($image);
    $width  $size[0];    // width of the image
    $height $size[1];  
            
    if(
    $size[0]>$size[1])
     
    $scale=$size[0]/100;
    else
     
    $scale=$size[1]/100;

                            
    $new_width$size[0]/$scale;
    $new_height=$size[1]/$scale;
                            
    $new_image imagecreatetruecolor($new_width$new_height);

    print 
    $new_image
    any ideas
    cheers


  •  

    Posting Permissions

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