...

View Full Version : resizing images using php.



LJackson
12-15-2008, 11:46 AM
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

abduraooft
12-15-2008, 12:02 PM
You could use getimagesize() (http://php.net/getimagesize) function to get the dimensions and then fund a scaling factor, say

$size= getimagesize($_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);

LJackson
12-15-2008, 12:41 PM
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,


$size= getimagesize($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

abduraooft
12-15-2008, 01:06 PM
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

LJackson
12-15-2008, 02:19 PM
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:


$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
$size= getimagesize ($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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum