All purpose resizing class / function

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

All purpose resizing class / function

Post 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?
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Ok, i was bored :P

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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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 :P)
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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 :lol:

Seems to work nicely now though :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply