dosberg Posted January 11, 2007 Share Posted January 11, 2007 Anyone know how I can add a crop function to Ciprian Voicu's Pictoru's Thumb class. The code is below. I would like to output an image that is cropped from the center of the image with equal height/width (a perfect square). Thanks!Script source: http://www.phpclasses.org/browse/package/3296.html[code]<?php# CLASS FILE/********************************************************************************* Name: Pictoru's Thumb ** Author: Ciprian Voicu ** Version: 1.0 ** Date: 2006-08-08 ** Description: • creates, saves, outputs thumbs ** License: GNU GPL *********************************************************************************/class pThumb { var $image; var $mime; var $imageName; var $maxHeight=150; var $maxWidth=200; var $quality=75; var $rotMode="CW"; var $rotColor="FFCC00"; # sets thumb max width function pSetWidth($val){ $this->maxWidth=$val; } # sets thumb max height function pSetHeight($val){ $this->maxHeight=$val; } # set thumb both width and height function pSetSize($width, $height){ $this->maxWidth=$width; $this->maxHeight=$height; } # set output quality function pSetQuality($number){ $this->quality=$number; } # destroy the resource function pDestroy(){ imagedestroy($this->image); } # rotate the image function pRotate($degrees, $mode=NULL, $bgcolor=NULL){ if($mode!=NULL) $this->rotMode=strtoupper($mode); if($bgcolor!=NULL) $this->rotColor=$bgcolor; $bg=base_convert($this->rotColor, 16, 10); $img=$this->image; switch($this->rotMode){ case "CW": case 1: $rotation=360-$degrees; break; case "CCW": case 2: $rotation=$degrees; break; } $img=imagerotate($this->image, $rotation, $bg); $this->image=$img; } # resize an image function pResize($img, $width, $height){ $img_sizes=getimagesize($img); switch($img_sizes[2]){ case 1: $source=imagecreatefromgif($img); $this->mime="GIF"; break; case 2: $source=imagecreatefromjpeg($img); $this->mime="JPG"; break; case 3: $source=imagecreatefrompng($img); $this->mime="PNG"; break; } $thumb=imagecreatetruecolor($width, $height); imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $img_sizes[0], $img_sizes[1]); $this->image=$thumb; } # create a thumb function pCreate($img, $maxwidth=NULL, $maxheight=NULL, $quality=NULL){ if($maxwidth!=NULL) $this->maxWidth=$maxwidth; if($maxheight!=NULL) $this->maxHeight=$maxheight; if($quality!=NULL) $this->quality=$quality; if(file_exists($img)){ $this->imageName=basename($img); $img_sizes = getimagesize($img); if($img_sizes[0] > $this->maxWidth || $img_sizes[1] > $this->maxHeight){ $percent_width = $this->maxWidth / $img_sizes[0]; $percent_height = $this->maxHeight / $img_sizes[1]; $percent=min($percent_width, $percent_height); }else{ $percent=1; } $width=$img_sizes[0]*$percent; $height=$img_sizes[1]*$percent; $this->pResize($img, $width, $height); }else{ $this->image=imagecreate($this->maxWidth, $this->maxHeight); $line=$this->pDecColors("FF0000"); $fill=$this->pDecColors("EFE6C2"); $fillcolor = imagecolorallocate($this->image, $fill['r'], $fill['b'], $fill['g']); $linecolor = imagecolorallocate($this->image, $line['r'], $line['b'], $line['g']); imagefill($this->image, 0, 0, $fillcolor); imagerectangle($this->image, 0, 0, ($this->maxWidth-1), ($this->maxHeight-1), $linecolor); imageline($this->image, 0, 0, $this->maxWidth, $this->maxHeight, $linecolor); imageline($this->image, 0, $this->maxHeight, $this->maxWidth, 0, $linecolor); } } # function taken from "http://ro2.php.net/manual/en/function.mkdir.php" function MakeDirectory($dir, $mode = 0777){ if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE; if (!$this->MakeDirectory(dirname($dir),$mode)) return FALSE; return @mkdir($dir,$mode); } # output the image function pOutput($print=true){ if($print!=true){ header('Content-type: application/force-download'); header('Content-Transfer-Encoding: Binary'); header('Content-Disposition: attachment; filename="'.$this->imageName.'"'); } switch($this->mime){ case "JPEG": case "JPG": header("Content-Type: image/jpeg"); imagejpeg($this->image, NULL, $this->quality); break; case "GIF": header("Content-Type: image/gif"); imagegif($this->image); break; case "PNG": header("Content-Type: image/png"); imagepng($this->image); break; default : header("Content-Type: image/gif"); imagegif($this->image); } $this->pDestroy(); } # save image function pSave($path){ if(!file_exists(dirname($path))){ $this->MakeDirectory(dirname($path)); } switch($this->mime){ case "JPEG": case "JPG": imagejpeg($this->image, $path, $this->quality); break; case "GIF": imagegif($this->image, $path); break; case "PNG": imagepng($this->image, $path); break; } } function pDecColors($color){ $color = str_replace("#", "", $color); $colors['r'] = hexdec(substr($color, 0, 2)); $colors['b'] = hexdec(substr($color, 2, 2)); $colors['g']= hexdec(substr($color, 4, 2)); return $colors; } }?>[/code] Link to comment https://forums.phpfreaks.com/topic/33761-thumbnail-class-for-creating-thumbs-from-uploaded-images/ Share on other sites More sharing options...
cheezylu Posted April 1, 2007 Share Posted April 1, 2007 I would like to know how to do this too! Link to comment https://forums.phpfreaks.com/topic/33761-thumbnail-class-for-creating-thumbs-from-uploaded-images/#findComment-219177 Share on other sites More sharing options...
cheezylu Posted April 1, 2007 Share Posted April 1, 2007 This may help.Here is a modified code from lixlpixel of PHP Paradise. This will create a cropped thumbnail of a JPEG and upload them both into a specified directory.[code]<?phpif(isset($_POST['Submit'])){ $size = 100; // the thumbnail height $filedir = 'pics/'; // the directory for the original image $thumbdir = 'pics/'; // the directory for the thumbnail image $prefix = 'THUM_'; // the prefix to be added to the original name $maxfile = '2000000'; $mode = '0666'; $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; if (isset($_FILES['image']['name'])) { $prod_img = $filedir.$userfile_name; $prod_img_thumb = $thumbdir.$prefix.$userfile_name; move_uploaded_file($userfile_tmp, $prod_img); chmod ($prod_img, octdec($mode)); $sizes = getimagesize($prod_img); $aspect_ratio_wide = $sizes[1]/$sizes[0]; $aspect_ratio_tall = $sizes[0]/$sizes[1]; if ($sizes[1] > $sizes[0]) // if height is greater than width{ if ($sizes[0] <= $size) // if width <= defined size { $new_width = $sizes[0]; $new_height = $sizes[1]; }else{ $new_width = $size; $new_height = abs($new_width/$aspect_ratio_tall); } $size_difference_height = $sizes[1] / $new_height; $center_width = 0; $center_height = (($new_height - $size) / 2) * $size_difference_height; } else if ($sizes[0] > $sizes[1]) { // else if width is greater than height if ($sizes[1] <= $size) { $new_width = $sizes[0]; $new_height = $sizes[1]; }else{ $new_height = $size; $new_width = abs($new_height/$aspect_ratio_wide); } $size_difference_width = $sizes[0] / $new_width; $center_width = (($new_width - $size) / 2) * $size_difference_width; $center_height = 0;} $destimg = imagecreatetruecolor($size,$size) or die('Problem In Creating image'); $srcimg = imagecreatefromjpeg($prod_img) or die('Problem In opening Source Image'); imagecopyresampled($destimg,$srcimg,0,0,$center_width,$center_height,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving'); imagedestroy($destimg); } echo ' <a href="'.$prod_img.'"> <img src="'.$prod_img_thumb.'" width="$size" height="$size"> </a> '; }else{ echo ' <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form>';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33761-thumbnail-class-for-creating-thumbs-from-uploaded-images/#findComment-219328 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.