Hello friends,
please am having problem with my image resize code the code is as below;
$originalImage = $_FILES["file"]["name"];
$toWidth = 300;
$toHeight = 300;
function resizeImage($originalImage,$toWidth,$toHeight){
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $imageResized;
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $imageResized);
$path2= "../upload/" . $imageResized;
but the picture is not resized, and if i use
$image = resizeImage()
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $image);
$path2= "../upload/" . $image;
no picture will be uploaded to my upload folder.
please what can i do or which other lesser stress way can i resize my picture.
Page 1 of 1
resize image problem
#2
Posted 30 August 2010 - 10:29 AM
Hi there,
You might like to try using something like this instead, it should do the trick if you're using PHP 5:
This will resize your image to a maximum of 300px while retaining the original dimensions.
If you're still having problems the manual for GD library is very helpful and not too hard to understand.
Hope it works out ok!
Kirsty
You might like to try using something like this instead, it should do the trick if you're using PHP 5:
$originalImage = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($originalImage);
list($width,$height) = getimagesize($originalImage);
$ratio = $width/$height;
if ($width > $height)
{if ($width > 300)
{$new_height = 300 / $ratio;
$new_width = 300;}
else
{$new_height = $height;
$new_width = $width;}}
else
{if ($height > 300)
{$new_width = 300 * $ratio;
$new_height = 300;}
else
{$new_height = $height;
$new_width = $width;}}
$target = "upload/image.jpg";
$tmp = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($tmp,$src,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($tmp,$target,100);
This will resize your image to a maximum of 300px while retaining the original dimensions.
If you're still having problems the manual for GD library is very helpful and not too hard to understand.
Hope it works out ok!
Kirsty
#4
Posted 01 September 2010 - 09:24 AM
Or even better use http://shiftingpixel...-image-resizer/ - really simple to use and has tons of features.
#6
Posted 03 September 2010 - 09:38 AM
zed, on 01 September 2010 - 09:27 AM, said:
why better? timthumb can resample in flight as well.
Calm down, was just saying
I always used the shifting pixel one I found it to be very lightweight and have all the features I need.
Although one extra feature that one has (that I probs wouldn't need) is the ability to specify a crop location and a filter, which is kinda nice. I'm sure they do the same job in the end.
Be happy, it's Friday and England are playing tonight
EDIT: one thing to remember is that all image reizers like this generate really messy urls in your webpage. I'd advise using some url rewriting to change something like
timthumb.php?src=lighthouse.jpg&a=r&w=60&h=130&f=3,80 into soemthing like
/images/thumbs/lighthouse.jpg
- ← PHP Contact form sends blank emails
- Server Side (PHP, Databases, ASP.NET, etc)
- Image Upload in a Form →
Share this topic:
Page 1 of 1
Help
















