Skip to content
Skip to search - Accesskey = s
parentNode.org
The building blocks of a solid frontend.
Pasted
by
berry__
on
2007-08-07 11:33
as
PHP
Create New
http://pastebin.parentnode.org/
Links this one
http://pastebin.parentnode.org/20365
Comment
GenerateThumb2 ( 100, 100 );
<?php class ImageObject { private $imageFilePath; private $source; private $newImage; private $imageWidth; private $imageHeight; private $thumb; public function ImageObject($myImage) { $this->imageFilePath = $myImage; $this->source = $this->getSourceImage(); list($width, $height) = getimagesize($this->imageFilePath); $this->imageWidth = $width; $this->imageHeight = $height; } private function getSourceImage() { $filetype = strrchr($this->imageFilePath, "."); if (strcasecmp($filetype, ".jpg") == 0 || strcasecmp($filetype, ".jpeg") == 0) { return imagecreatefromjpeg($this->imageFilePath); } else if (strcasecmp($filetype, ".png") == 0) { return imagecreatefrompng($this->imageFilePath); } else if (strcasecmp($filetype, ".gif") == 0){ return imagecreatefromgif($this->imageFilePath); } else { return false; } } public function generateThumb($width, $height) { // $this -> imageWidth = 480 // $this -> imageHeight = 440 // $width = 100 // $height = 100 //check if the image is too big if($this->imageWidth > $width || $this->imageHeight > $height) { // true. //set cropWidth & height $cropWidth = $this->imageWidth / $width; // 480 / 100 = 4.8 $cropHeight = $this->imageHeight / $height; // 440 / 100 = 4.4 //set starting x & y positions for cropping $x = 0;// $cropWidth / 2; $y = 0; //$cropHeight / 2; //create blank image to put the new thumb on $this->newImage = imagecreatetruecolor($width, $height); //copy the thumb to to imagecopyresampled($this->newImage, $this->source, 0, 0, $x, $y, $width, $height, $this->imageWidth, $this->imageHeight); } else { return; } } public function generateThumb2 ( $width, $height ) { // Is the image too big? if ( $this -> imageWidth > $width OR $this -> imageHeight > $height ) { // true. $dst_image = imagecreatetruecolor ( $width, $height ); $this -> newImage =& $dst_image; $src_image = $this -> source; $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; $dst_w = $width; $dst_h = $height; $src_w = $this -> imageWidth; $src_h = $this -> imageHeight; /** * So, i want the horizontal center, as well as the vertical center, * plus I want to crop off half op the image. */ $src_w = $src_w / 2; // 448 / 2 = 224 $src_h = $src_h / 2; // 440 / 2 = 220 /** * Because the height is now 224x220, the x and y axis are on 224 and 220. * That means the image will be centered as well. **/ $src_x = 224; $src_y = 220; imagecopyresampled ( $dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); } } public function output () { header('Content-type: image/jpeg'); imagejpeg ( $this -> newImage, null, 100 ); } public function destroy() { imagedestroy($this->source); } } $image = new ImageObject ( './284521.jpg' ); $image -> generateThumb2 ( 100, 100 ); $image -> output (); ?>