Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2008, 11:46 AM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
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
LJackson is offline   Reply With Quote
Old 12-15-2008, 12:02 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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); 
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-15-2008, 12:41 PM   PM User | #3
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
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
LJackson is offline   Reply With Quote
Old 12-15-2008, 01:06 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 12-15-2008 at 02:36 PM..
abduraooft is offline   Reply With Quote
Old 12-15-2008, 02:19 PM   PM User | #5
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
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
LJackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:58 AM.


Advertisement
Log in to turn off these ads.