Page 1 of 1
All purpose resizing class / function
Posted: Mon Jul 24, 2006 4:50 am
by shiznatix
Is there a class or function that is general purpose like will take any type of image (within reason), make it a new x and y size, then save the file to a specified path (possibly overwriting the old file, possibly saving a new file).
something like:
Code: Select all
require_once 'imageStuff.php';
$editor = new ImageStuff;
$editor->OriginalImage = './Images/something.jpg';
$editor->ImageType = 'jpeg';
$editor->NewX = '500';
$editor->NewY = '400';
$editor->SavePath = './Images/something_new.jpg';
$editor->Execute();
does this type of class exist anywhere?
Posted: Mon Jul 24, 2006 4:57 am
by MarK (CZ)
Some similar may exist but I don't think it's that hard to write own one.
It's gonna be just a few lines... One function with four parameters would do it.
Posted: Mon Jul 24, 2006 5:16 am
by MarK (CZ)
Ok, i was bored
This should work as intended:
Code: Select all
function ImageResize($src, $out, $outX, $outY) {
// get source image properties
$dim = @getimagesize($src);
if ($dim === FALSE)
trigger_error("Failed to read the source image", E_USER_ERROR);
list($srcX, $srcY, $srcType) = $dim;
// create images
switch ($srcType) {
case 1 : $imgSrc = @imagecreatefromgif($src); break; // GIF
case 2 : $imgSrc = @imagecreatefromjpeg($src); break; // JPG
case 3 : $imgSrc = @imagecreatefrompng($src); break; // PNG
default: trigger_error("Unsupported image type", E_USER_ERROR);
}
$imgOut = @imagecreatetruecolor($outX, $outY);
if ($imgSrc === FALSE || $imgOut === FALSE)
trigger_error("Failed to create image", E_USER_ERROR);
// copy and resize image
if (@imagecopyresampled($imgOut, $imgSrc, 0, 0, 0, 0, $outX, $outY, $srcX, $srcY)
=== FALSE)
trigger_error("Failed to resize image", E_USER_ERROR);
// save image
switch ($srcType) {
case 1 : $saveres = @imagegif($imgOut, $out); break; // GIF
case 2 : $saveres = @imagejpeg($imgOut, $out); break; // JPG
case 3 : $saveres = @imagepng($imgOut, $out); break; // PNG
default: trigger_error("Unsupported image type", E_USER_ERROR);
}
if ($saveres === FALSE)
trigger_error("Failed to save image", E_USER_ERROR);
return true;
}
Update the switches for filetypes you need.
Posted: Mon Jul 24, 2006 6:12 am
by shiznatix
w00t thank you very much. i probably shouldn't be so scared of anything to do with images but I usually just suck at them and never know what I am doing.
anyway, thank you very much, works perfect (at least for jpegs so far

)
Posted: Mon Jul 24, 2006 7:01 am
by MarK (CZ)
I like working with images, it's fun.
Last time I was making a map, where I needed to place spots on a map of czech republic.
I wanted it to work just with GPS coordinates, so I needed recounting of it to image dimensions.
Problems arised: I didn't know what are the GPS coords of [x: 0, y: 0] spot, or any other spots. Later I found out that the map is also rotated compared to the GPS system. Also north axis is flipped to the y axis in image. And other happy probs, needed lots of math
Seems to work nicely now though

Posted: Mon Jul 24, 2006 10:27 am
by onion2k
http://www.phpgd.com/scripts.php?script=1 .. That sort of meets your criteria.
http://www.phpgd.com/scripts.php?section=2 .. There's a bunch of thumbnail objects I've written that extend it.