How to resize and save image
Tue 29 Sep 2015, 06:47
This is my function to resize and save image in php:
function resizeImage($filename, $saveto, $max_width, $max_height)
{
list($orig_width, $orig_height) = getimagesize($filename);
$width = $orig_width;
$height = $orig_height;
# taller
if ($height > $max_height) {
$width = ($max_height / $height) * $width;
$height = $max_height;
}
# wider
if ($width > $max_width) {
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image_p = imagecreatetruecolor($width, $height);
$exts=explode(".", $saveto);
$ext=$exts[count($exts) - 1];
switch(strtolower($ext)){
case "jpg":
$image = imagecreatefromjpeg($filename);
break;
case "jpeg":
$image = imagecreatefromjpeg($filename);
break;
case "png":
$image = imagecreatefrompng($filename);
break;
case "gif":
$image = imagecreatefromgif($filename);
break;
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
switch(strtolower($ext)){
case "jpg":
imagejpeg($image_p,$saveto,100);
break;
case "jpeg":
imagejpeg($image_p,$saveto,100);
break;
case "png":
imagepng($image_p,$saveto,100);
break;
case "gif":
imagegif($image_p,$saveto,100);
break;
}
}
example:
resizeImage($file[‘post variable']['tmp_name’],$destinationpath, 800, 800);
0
5091 views