|
Posted by Dimitri Marshall on 07/31/06 04:26
Thanks guys,
I got it figured out before reading your posts, but I used a different code
than the one I posted. The one I'm using only allows for JPEGs so I'm gonna
see what I can use from what you have shown me.
Thanks again,
--
Dimitri Marshall
"MS" <NoSpam@spamless.com> wrote in message
news:5bWdnSiNtZDRUlHZRVnysA@bt.com...
> "Dimitri Marshall" <recipient@shawRemoveThis.ca> wrote in message
> news:DyQyg.265981$Mn5.160120@pd7tw3no...
>> Ok, I found some code to resize an image (to make it a thumbnail), but I
>> cannot figure out how to make that image save the my server. Any ideas?
>
> Somebody on this newsgroup recently uploaded a class for image uploading,
> which i thought was great, I have improved it with extra things.
>
> It probably can be improved more but it works for my needs.
>
> Features...
> Uploads .jpg .jpeg .png .gif extensions
> checks for file MIME types
> "image/gif"
> "image/jpg"
> "image/png"
> "image/x-png"
> "image/pjpeg"
> "image/pjpeg;"
> "image/jpeg"
>
> Tested in both IE6 and Firefox 1.5.0.5
>
> Option to add a Watermark image at bottom right. Watermark size is a % of
> the uploaded image.
>
> Thumbnail Class included
>
> if thumbnail exists at requested size it is used if not the thumbnail is
> created and then used.
>
> Also a delete function that will delete the original image with option to
> delete the thumbs associated with the image.
>
>
> after upload form has been submitted.
> $picture = new picture($save_directory, $filename, $_FILES['new_image'],
> $maxwidth, $maxheight, $watermark, $watermarksize, $maxfilesize);
>
> // $save_directory - "path/to/dir/"
> // $filename - the filename you want to save as, without the extension.
> "pic_1" extension will be added dependant upon the uploaded file.
> // $_FILES['new_image'] -
> // $maxwidth
> // $maxheight - the image is resized to $maxwidth pixels x $maxheight
> pixels
> keeping the aspect ratio only if the uploaded image is larger than
> $maxwidth
> by $maxheight
> // $watermark - "path/to/watermark/image.jpg"
> // $watermarksize - "5" watermark image will be sized at $watermarksize %
> of
> uploaded image.
> // $maxfilesize - number of bytes allowed to be uploaded - if 0 no limit;
>
>
> To create a thumbnail
> $thumb = new thumb();
> $thumb->thumbnail($original_image_name, $thumbwidth, $thumbheight);
>
> // $original_image_name - "path/to/original/image.jpg"
> // $thumbwidth - max Width of Thumbnail
> // $thumbheight - max Height of thumbnail - will only thumbnail if
> original
> image is larger than $thumbwidth by $thumbheight
> // if a thumb of $thumbwidth by $thumbheight is not yet created it will
> create it in the same location as the original image with a filename...as
> follows...
> // original name "image.jpg"
> // thumbnail name "image_tn150x150.jpg"
> // where the 150x150 = $thumbwidthx$thumbheight
>
>
> to delete an image.
> $thumb = new thumb();
> $thumb->DeleteImages($original_image_name, $deletethumbs);
>
> // $original_image_name - "path/to/original/image.jpg"
> // $deletethumbs - Bool - true/false weather or not to delete the
> thumbnails
> created from image "$original_image_name".
>
> I hope this helps somebody out !!
>
> <?php
> class picture{
> var $save_dir; // where file will be saved
> var $filename="spacer.gif"; // default file name initially
> var $error_message=""; // string to be output if neccesary
> var $width; // height of final image
> var $height; // width of final image
> var $type; // File type "image/jpg" etc..
>
> function picture($save_directory, $filename, $file_array,
> $max_width, $max_height, $watermark, $watermarksize="5",
> $filesize="0"){
> $this->save_dir = $save_directory;
> $this->width = $max_width;
> $this->height = $max_height;
> $this->type = $file_array['type'];
> $this->MakeDirectory($this->save_dir, 0755);
>
> if($file_array['error'] == 0){
> if(($filesize == 0) || ($file_array['size'] < $filesize)){
> //--change filename to time - make it unique
> $temp_filename = strtolower($file_array['name']);
> $ext = explode('.',$temp_filename);
> $ext = $ext[count($ext)-1];
> $temp_filename = $filename == "" ? time().".".$ext :
> $filename.".".$ext;
>
> //--check that it's a jpeg or gif or png
> if((preg_match('/^(gif|jpeg|jpg|png)$/',$ext)) &&
> (($this->type == "image/gif") ||
> ($this->type == "image/jpg") ||
> ($this->type == "image/png") ||
> ($this->type == "image/x-png") ||
> ($this->type == "image/pjpeg") ||
> ($this->type == "image/pjpeg;") ||
> ($this->type == "image/jpeg"))){
>
> // resize in proportion if oversized
> list($width_orig, $height_orig) =
> getimagesize($file_array['tmp_name']);
>
> if(($width_orig > $this->width) || ($height_orig >
> $this->height)){
> // Image is Oversized resize
> if($this->width && ($width_orig < $height_orig)){
> $this->width = ($this->height / $height_orig) *
> $width_orig;
> }else{
> $this->height = ($this->width / $width_orig) *
> $height_orig;
> }
> }else{
> // Image is smaller than max allowed.
> // Keep Original Size
> $this->width = $width_orig;
> $this->height = $height_orig;
> }
>
> // check width and height for values.
> // if blank - possible wrong file type with jpg/gif extension.
> if(($this->width > 0) && ($this->height > 0)){
> $image_p = imagecreatetruecolor($this->width, $this->height);
>
> //handle gifs PNGs and jpegs separately
> if($ext=='gif'){
> $image = imagecreatefromgif($file_array['tmp_name']);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width,
> $this->height, $width_orig, $height_orig);
> //////////
> // Watermark
> //////////
> if(($watermark != "") && (file_exists($watermark))){
> // creat a function for watermarking.
> $image_p = $this->AddWatermark($image_p, $watermark,
> $watermarksize);
> }
> //////////
> // end Watermark
> //////////
> imagegif($image_p, $this->save_dir.$temp_filename, 80);
> }elseif($ext=='png'){
> $image = imagecreatefrompng($file_array['tmp_name']);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width,
> $this->height, $width_orig, $height_orig);
> //////////
> // Watermark
> //////////
> if(($watermark != "") && (file_exists($watermark))){
> // creat a function for watermarking.
> $image_p = $this->AddWatermark($image_p, $watermark,
> $watermarksize);
> }
> //////////
> // end Watermark
> //////////
> imagepng($image_p, $this->save_dir.$temp_filename, 80);
> }else{
> $image = imagecreatefromjpeg($file_array['tmp_name']);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width,
> $this->height, $width_orig, $height_orig);
> //////////
> // Watermark
> //////////
> if(($watermark != "") && (file_exists($watermark))){
> // creat a function for watermarking.
> $image_p = $this->AddWatermark($image_p, $watermark,
> $watermarksize);
> }
> //////////
> // end Watermark
> //////////
> imagejpeg($image_p, $this->save_dir.$temp_filename, 80);
> }
>
> imagedestroy($image_p);
> imagedestroy($image);
>
> $this->filename=$temp_filename;
> }else{
> $this->error_message = $this->GetError("22");
> }
> }else{
> $this->error_message = $this->GetError("20");
> }
> }else{
> $this->error_message = $this->GetError("21",$filesize);
> }
> }else{
> $this->error_message = $this->GetError($file_array['error']);
> }
> }
>
> function AddWatermark($image, $watermark, $watermarksize){
> // Create a Watermark bottom right, scale to $watermarksize and merge
>
> // create new size for watermark
> list($wm_width, $wm_height) = getimagesize($watermark);
> if($this->width > $this->height){
> $wm_new_width = intval(($this->width / 100) * $watermarksize);
> $wm_new_height = ($wm_new_width / $wm_width) * $wm_height;
> }else{
> $wm_new_height = intval(($this->height / 100) * $watermarksize);
> $wm_new_width = ($wm_new_height / $wm_height) * $wm_width;
> }
>
> //handle gifs PNGs and jpegs separately
> if($ext=='gif'){
> $image_wm = imagecreatefromgif($watermark);
> }elseif($ext=='png'){
> $image_wm = imagecreatefrompng($watermark);
> }else{
> $image_wm = imagecreatefromjpeg($watermark);
> }
> // create empty resource
> $image_wm_tn = imagecreatetruecolor($wm_new_width, $wm_new_height);
> imagecopyresampled($image_wm_tn, $image_wm, 0, 0, 0, 0, $wm_new_width,
> $wm_new_height, $wm_width, $wm_height);
> imagecopymerge($image,
> $image_wm_tn,
> $this->width-5-$wm_new_width,
> $this->height-5-$wm_new_height,
> 0, 0, $wm_new_width, $wm_new_height, 65 );
>
> return $image;
> }
>
>
> function GetError($errnum, $filesize=""){
> if(($errnum != "") && ($errnum != 0)){
> // Standard Error Messages
> $errmes[1] = "The uploaded file exceeds the allowed file size limit.";
> $errmes[2] = "The uploaded file exceeds the allowed file size limit.";
> $errmes[3] = "The uploaded file was only partially uploaded.";
> $errmes[4] = "No file was uploaded.";
> $errmes[5] = "";
> $errmes[6] = "Missing a temporary folder.";
> $errmes[7] = "Failed to write file to disk.";
> // Class Error Messages
> $errmes[20] = "The uploaded file does not have a .gif - .jpg - .jpeg -
> .png file extension.";
> $errmes[21] = "The uploaded file exceeds the allowed file size of
> ".number_format($filesize)." bytes.";
> $errmes[22] = "The uploaded file does not seem to be a valid picture
> type.";
> return $errmes[$errnum];
> }
> }
>
>
> function MakeDirectory($dir, $mode = 0755){
>
> if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
> if (!$this->MakeDirectory(dirname($dir),$mode)) return FALSE;
>
> return @mkdir($dir,$mode);
> }
> }
>
> class thumb{
> function thumbnail($filename,$width,$height){
> // check file exists
> if(file_exists($filename)){
> //check if thumbnail already exists
> $ext = explode('.',$filename);
> $ext = $ext[count($ext)-1];
> $extlen = strlen($ext)+1;
> $thumbname = substr($filename, 0,
> strlen($filename)-$extlen)."_tn".$width."x".$height.".".$ext;
> if(!file_exists($thumbname)){
> // create thumbnail image
> if(preg_match('/^(gif|jpeg|jpg|png)$/',$ext)){
> // resize in proportion if oversized
> list($width_orig, $height_orig) = getimagesize($filename);
>
> if(($width_orig > $width) || ($height_orig > $height)){
> // Image is Oversized resize
> if($width && ($width_orig < $height_orig)){
> $width = ($height / $height_orig) * $width_orig;
> }else{
> $height = ($width / $width_orig) * $height_orig;
> }
> }else{
> // Image is smaller than Thumb size required.
> // Keep Original Size
> $width = $width_orig;
> $height = $height_orig;
> }
>
> // check width and height for values.
> // if blank - possible wrong file type with jpg/gif extension.
> if(($width > 0) && ($height > 0)){
> $image_p = imagecreatetruecolor($width, $height);
>
> //handle gifs and jpegs separately
> if($ext=='gif'){
> $image = imagecreatefromgif($filename);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
> $width_orig, $height_orig);
> imagegif($image_p, $thumbname, 80);
> }elseif($ext=='png'){
> $image = imagecreatefrompng($filename);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
> $width_orig, $height_orig);
> imagepng($image_p, $thumbname, 80);
> }else{
> $image = imagecreatefromjpeg($filename);
> imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
> $width_orig, $height_orig);
> imagejpeg($image_p, $thumbname, 80);
> }
>
> imagedestroy($image_p);
> imagedestroy($image);
> }
> }
> }
> return $thumbname;
> }
> }
>
> function DeleteImages($image,$thumbs=true){
> // Deletes images and thumbs if $thumbs set to true
> if((file_exists($image)) && ($thumbs == true)){
> $dir = dirname($image);
> $temp_filename = substr($image,strlen($dir));
> $ext = explode('.',$temp_filename);
> $ext = $ext[count($ext)-1];
> $extlen = strlen($ext)+1;
> $image_prefix = substr($temp_filename, 0,
> strlen($filename)-$extlen)."_tn"."*.".$ext;
> //die($dir.$image_prefix);
> foreach (glob($dir.$image_prefix) as $filename) {
> @unlink($filename);
> }
> }
> @unlink($image);
> }
> }
> ?>
>
>
>
[Back to original message]
|