PHP - resize image problem - Asked By Olusegun A. on 28-Aug-10 11:54 AM

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.
Super Man replied to Olusegun A. on 28-Aug-10 12:03 PM
01.i am not good in php.
02.but provide you one sample code for that:
03.  
04.function makeimage($filename, $newfilename, $path, $newwidth, $newheight) { 
05.  
06.  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . ) 
07.  $image_type = strstr($filename'.'); 
08.  
09.  //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION 
10.    switch($image_type) { 
11.      case '.jpg'
12.        $source = imagecreatefromjpeg($filename); 
13.        break
14.      case '.png'
15.        $source = imagecreatefrompng($filename); 
16.        break
17.      case '.gif'
18.        $source = imagecreatefromgif($filename); 
19.        break
20.      default
21.        echo("Error Invalid Image Type"); 
22.        die
23.        break
24.      
25.     
26.  //CREATES THE NAME OF THE SAVED FILE 
27.  $file = $newfilename . $filename
28.     
29.  //CREATES THE PATH TO THE SAVED FILE 
30.  $fullpath = $path . $file
31.  
32.  //FINDS SIZE OF THE OLD FILE 
33.  list($width$height) = getimagesize($filename); 
34.  
35.  //CREATES IMAGE WITH NEW SIZES 
36.  $thumb = imagecreatetruecolor($newwidth$newheight); 
37.  
38.  //RESIZES OLD IMAGE TO NEW SIZES 
39.  imagecopyresized($thumb$source,  0,  0,  0,  0,  $newwidth$newheight$width$height); 
40.  
41.  //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100 
42.  imagejpeg($thumb$fullpath,  60); 
43.  
44.  //CREATING FILENAME TO WRITE TO DATABSE 
45.  $filepath = $fullpath
46.     
47.  //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION 
48.  return $filepath
49.  
50.
51.  
52.?>

you can also visit this good link:

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php