...

View Full Version : Image resize function



tomharto
04-21-2011, 07:41 AM
Is there a PHP function to resize an image? I've googled it and found pre made scripts to do it but i wanna have a go at making one. All im after is the function name like "resizeimage()",so I can google it at find out about it. Thanks :)

BoyWander
04-21-2011, 09:22 AM
You can put html inside of php...

tomharto
04-21-2011, 10:24 AM
Sorry, i wasnt being clear >.<. I mean like for when someone uploads an image, if they attach an image 10000x10000 it will be resized to 1000x1000. Thats the kinda resize i mean.

angst
04-21-2011, 03:14 PM
try this function, it requires imagick to work:



function ResizeImage($saveToDir, $imagePath, $imageName, $max_x, $max_y){
if (file_exists($imagePath)) {

$image = new Imagick($imagePath);
$height = $image->getImageHeight();
$width = $image->getImageWidth();
if ($width > $max_x) $image->scaleImage($max_x,$max_y,true);
if ($height > $max_y) $image->scaleImage($max_x,$max_y,true);
$image->writeImage($saveToDir . $imageName);
}
}

tomharto
04-21-2011, 03:37 PM
Thanks, ill give that a go when i get home :). Currently stuck at work when it warm and sunny outside -.-

Keleth
04-21-2011, 04:16 PM
I'm sure this can be simplified, but if you don't have imagik, here's what I use:


list($imgWidth, $imgHeight, $imgType) = getimagesize($source);
if (image_type_to_mime_type($imgType) == 'image/jpeg' || image_type_to_mime_type($imgType) == 'image/pjpeg') $tempImg = imagecreatefromjpeg($source);
elseif (image_type_to_mime_type($imgType) == 'image/gif') $tempImg = imagecreatefromgif($source);
elseif (image_type_to_mime_type($imgType) == 'image/png') $tempImg = imagecreatefrompng($source);

$xRatio = $maxWidth / $imgWidth;
$yRatio = $maxHeight / $imgHeight;

if ($imgWidth <= $maxWidth && $imgHeight <= $maxHeight) {
$finalWidth = $imgWidth;
$finalHeight = $imgHeight;
} elseif (($xRatio * $imgHeight) < $maxHeight) {
$finalWidth = $maxWidth;
$finalHeight = ceil($xRatio * $imgHeight);
} else {
$finalWidth = ceil($yRatio * $imgWidth);
$finalHeight = $maxHeight;
}

$tempColor = imagecreatetruecolor($finalWidth, $finalHeight);
imagecopyresampled($tempColor, $tempImg, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight);

if ($extension == 'jpg' || $extension == 'jpeg') imagejpeg($tempColor, $destination, 100);
elseif ($extension == 'gif') imagegif($tempColor, $destination);
elseif ($extension == 'png') imagepng($tempColor, $destination);
imagedestroy($tempImg);
imagedestroy($tempColor);

As you can see, this come from a use where you can change the max height/width (through a function).

angst
04-21-2011, 04:22 PM
here's a good function that uses GD



function ResizeImage($saveToDir, $imagePath, $imageName, $max_x, $max_y){
preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageName, $ext);
switch (strtolower($ext[2])) {
case 'jpg' : $im = imagecreatefromjpeg ($imagePath);
break;
case 'jpeg': $im = imagecreatefromjpeg ($imagePath);
break;
case 'gif' : $im = imagecreatefromgif ($imagePath);
break;
case 'png' : $im = imagecreatefrompng ($imagePath);
break;
default : $stop = true;
break;
}

if (!isset($stop)) {

$x = imagesx($im);
$y = imagesy($im);

if (($max_x/$max_y) < ($x/$y)) {
$save = imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x));
} else {
$save = imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y));
}
imagecopyresized($save, $im, 0, 0, 0, 0, imagesx($save), imagesy($save), $x, $y);

switch (strtolower($ext[2])) {
case 'jpg' : imagejpeg($save, "{$saveToDir}{$ext[1]}.".$ext[2]."",100);
break;
case 'jpeg': imagejpeg($save, "{$saveToDir}{$ext[1]}.".$ext[2]."",100);
break;
case 'gif' : imagegif($save, "{$saveToDir}{$ext[1]}.".$ext[2]."");
break;
case 'png' : imagepng($save, "{$saveToDir}{$ext[1]}.".$ext[2]);
break;
}
imagedestroy($im);
imagedestroy($save);
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum