August 28, 201016 yr 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.
August 30, 201015 yr Hi there, 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
Create an account or sign in to comment