![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
Batch optimizing and watermarking images using php
Requires PHP and the GD Libraries.
Optimize images class: Code:
<?php namespace sarettahs_image_script; // class to create optimized images and thumbnails from original images. Sarettah // program expects image folder to be structured as image_folder/set_folder/image // accepts jpg, gif or png input // creates jpg, gif or png output class optimize_images { public $verbose=0; // optimized image dimensions public $maxheight=1000; public $maxwidth=1000; // thumb dimensions public $thumbheight=350; public $thumbwidth=350; // types of images to process public $imagelist='jpg,jpeg,gif,png'; // type of image to create can be jpg, gif, png public $image_output='jpg'; // folders to use public $base_dir=''; public $optimized_dir=''; public $thumb_dir=''; // watermark image to use public $watermark=''; // err report public $errmess=''; // counts public $tot_files_made=0; public $tot_files_failed=0; public $tot_thumbs_made=0; public $tot_thumbs_failed=0; protected $make_thumbs=0; protected $folder_list=array(); protected $file_list=array(); protected $files_made=0; protected $files_failed=0; protected $thumbs_made=0; protected $thumbs_failed=0; public function mainline() { if(substr_count($this->imagelist,$this->image_output)==0) { $image_output='jpg'; } // error out if base folder or optimized folder is not defined. if(empty($this->base_dir)) { $this->errmess .="No Base Folder Defined<br>\n"; } if(!is_dir($this->base_dir)) { $this->errmess .="Base Folder " . $this->base_dir . " does not exist<br>\n"; } if(empty($this->optimized_dir)) { $this->errmess .="No Optimized Folder Defined<br>\n"; } elseif(!is_dir($this->optimized_dir)) { mkdir($this->optimized_dir); if(!is_dir($this->optimized_dir)) { $this->errmess .="Could not create folder " . $this->optimized_dir . "<br>\n"; } } // if no errors then go ahead and start it up if(empty($this->errmess)) { // if thumb directory is defined then we will make thumbs if(!empty($this->thumb_dir)) { if(!is_dir($this->thumb_dir)) { mkdir($this->thumb_dir); } if(!is_dir($this->thumb_dir)) { $this->errmess .="Could not create folder " . $this->thumb_dir . "<br>\n"; $this->make_thumbs=0; } else { $this->make_thumbs=1; } } else { $this->make_thumbs=0; } $this->process_images(); } } protected function process_images() { set_time_limit(0); // get the folders inside the base folder $this->get_folders(); if($this->verbose) { echo "found " . count($this->folder_list) . " folders<br>\n"; flush(); } // process through each folder in base folder for images foreach($this->folder_list as $folder) { if($this->verbose) { echo "Getting files for folder " . $folder . "<br>\n"; flush(); } $this->process_folder($this->base_dir, $folder); } } protected function process_folder($basedir, $folder) { $dir2use=$basedir . $folder . "/"; // get a list of the files in the folder $this->get_files($dir2use); if($this->verbose) { echo "Found " . count($this->file_list) . " files<br>\n"; flush(); } // initialize counters $this->files_made=0; $this->files_failed=0; $this->thumbs_made=0; $this->thumbs_failed=0; foreach($this->file_list as $image) { $this->make_optimized($dir2use, $image, $this->optimized_dir . $folder . '/', $this->maxheight, $this->maxwidth, $this->watermark); $image2chk=substr($image,0,strrpos($image,'.')) . '.' . $this->image_output; if(is_file($this->optimized_dir . $folder . '/' . $image2chk)) { $this->files_made++; } else { $this->files_failed++; } if($this->make_thumbs) { $this->make_optimized($dir2use, $image, $this->thumb_dir . $folder . '/', $this->thumbheight, $this->thumbwidth, $this->watermark); $image2chk=substr($image,0,strrpos($image,'.')) . '.' . $this->image_output; if(is_file($this->thumb_dir . $folder . '/' . $image2chk)) { $this->thumbs_made++; } else { $this->thumbs_failed++; } } } if($this->verbose) { echo "files made=" . $this->files_made . "<br>\n"; echo "files failed=" . $this->files_failed . "<br>\n"; echo "Thumbs made=" . $this->thumbs_made . "<br>\n"; echo "Thumbs Failed=" . $this->thumbs_failed . "<br>\n"; } $this->tot_files_made +=$this->files_made; $this->tot_files_failed +=$this->files_failed; $this->tot_thumbs_made +=$this->thumbs_made; $this->tot_thumbs_failed +=$this->thumbs_failed; } protected function make_optimized($dir2use, $file_in, $newdir, $maxheight, $maxwidth, $watermark) { $imagename=$dir2use . $file_in; // create new name using image extension $newname=substr($file_in,0,strrpos($file_in,'.')) . '.' . $this->image_output; $newname=$newdir . $newname; $width2use=0; $height2use=0; $ratio=0.0000; if(is_file($imagename)) { $copyit=0; list($origwidth, $origheight, $type, $attr) = getimagesize($imagename); if($origwidth>0 && $origheight>0) { if($origheight<=$maxheight && $origwidth<=$maxwidth) { // if the image coming in is smaller than our optimized size then we just want to copy the image instead of re-sizing it $copyit=1; } // if the image coming in is smaller than our optimized size then we want to compute a ratio to use for re-sizing the image if($origheight>$origwidth) { if($origheight<$maxheight) { $maxheight=$origheight; } $ratio=$maxheight/$origheight; $width2use=$origwidth * $ratio; $height2use=$maxheight; } else { if($origwidth<$maxwidth) { $maxwidth=$origwidth; } $ratio=$maxwidth/$origwidth; $height2use=$origheight * $ratio; $width2use=$maxwidth; } if(!is_dir($newdir)) { mkdir($newdir); } if(is_dir($newdir)) { $right=20; $bottom=20; $imagestr=file_get_contents($imagename); $image2use=imagecreatefromstring($imagestr); $imagestr=''; $use_watermark=0; if(!empty($watermark) && is_file($watermark)) { $use_watermark=1; $imagestr=file_get_contents($watermark); $watermark2use=imagecreatefromstring($imagestr); $imagestr=''; } if($use_watermark) { // place watermark onto image imagecopy($image2use, $watermark2use, imagesx($image2use) - imagesx($watermark2use) - $right, imagesy($image2use) - imagesy($watermark2use) - $bottom, 0, 0, imagesx($watermark2use), imagesx($watermark2use)); } if($copyit) { // copy image without resizing it $this->make_image($image2use, $newname); } else { // copy image while resizing it $newimage = imagecreatetruecolor($width2use, $height2use); imagecopyresampled($newimage, $image2use, 0, 0, 0, 0, $width2use, $height2use, $origwidth, $origheight); $this->make_image($newimage, $newname); imagedestroy($newimage); } imagedestroy($image2use); if($use_watermark) { imagedestroy($watermark2use); } if(!is_file($newname)) { if($copyit) { $this->errmess .="Could not copy image " . $newname . "<br>\n"; } else { $this->errmess .="Could not create image " . $newname . "<br>\n"; } } } else { $this->errmess .="Could not create directory " . $newdir . "<br>\n"; } } } } protected function make_image($image2use, $newname) { if($this->image_output=='jpg') { imagejpeg($image2use,$newname); } elseif($this->image_output=='gif') { imagegif($image2use,$newname); } elseif($this->image_output=='png') { imagepng($image2use,$newname); } } protected function get_folders() { $folderlist=scandir($this->base_dir); foreach($folderlist as $folder) { if(trim($folder)<>'..' && trim($folder)<>'.') { $path2chk=$this->base_dir . $folder . '/'; if(is_dir($path2chk)) { $this->folder_list[]=trim($folder); } } } } protected function get_files($pathin) { if(is_dir($pathin)) { $filelist=scandir($pathin); foreach($filelist as $file) { $file2chk=$pathin . $file; if(is_file($file2chk)) { // only return image files if(substr_count($this->imagelist,strtolower(trim(pathinfo($file2chk)['extension'])))>0) { $this->file_list[]=trim($file); } } } } } } ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
USAGE:
Code:
<?php require_once('optimize_images_class.php'); // create class $new_page=new sarettahs_image_script\optimize_images; $new_page->verbose=1; // show messages as the run progresses. if 0 then clas will not produce run messages $new_page->base_dir='/image_folder/'; // folder where original images are. This folder must exist $new_page->optimized_dir='/optimized_folder/'; // folder to put resized images in. Program will try to create this folder if it is not there $new_page->thumb_dir='/thumbs_folder/'; // folder to put thumbs into. Program will try to create this folder if not there. If left blank then will not create thumbs. $new_page->watermark='watermark_image'; // image to use for watermark. If left blank then no watermark will be used $new_page->image_output='gif'; // can be jpg, gif or png echo "Start of run " . date("M d Y H:i:s", time()) . "<br>\n"; flush(); $new_page->mainline(); // runs the program if(!empty($new_page->errmess)) { echo "Errors: " . $new_page->errmess . "<br>\n"; // lists errors } else { echo "There were no errors<br>\n"; } echo "End of run " . date("M d Y H:i:s", time()) . "<br>\n"; // show the total counts echo "Made " . $new_page->tot_files_made . " optimized images<br>\n"; echo $new_page->tot_files_failed . " optimized images failed<br>\n"; echo "Made " . $new_page->tot_thumbs_made . " thumb images<br>\n"; echo $new_page->tot_thumbs_failed . " thumb images failed<br>\n"; ?> ![]() .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
And yes, I know there are other people's versions out there and they have been out there for years.
This happens to be my version and I have been using it in various forms for a lot of years now. I probably need to change the padding and position of the watermark to make it dynamic. Right now it is hardcoded to be in the bottom right corner 20pixels up from the bottom and 20 pixels in from the right. That is where I always put my watermark. .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Aug 2005
Location: YUROP
Posts: 8,595
|
Nice share ;)
__________________
![]() Anal Webcams | Kinky Trans Cams Live | Hotwife XXX Tube | Get your Proxies here |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
<&(©¿©)&>
Industry Role:
Join Date: Jul 2002
Location: Chicago
Posts: 47,882
|
Looks good, but GD is crap though, you should rewrite it to use imagemagick instead...
![]()
__________________
Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000 Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager ![]() Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
Thanks
Quote:
What, in your opinion, are the advantages of using imagemagick versus the GD libraries? .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
<&(©¿©)&>
Industry Role:
Join Date: Jul 2002
Location: Chicago
Posts: 47,882
|
It's just better overall... it's faster, lower memory usage, more robust, easier to work with, easier to install, has more options, creates better quality images, etc...
for example, making a thumb in imagemagick is one line of code, and you can easily sharpen it, specify quality, size, etc... with GD all that is a pain in the ass, even something as trivial as moving the watermark to the top right instead or resizing it at the same time becomes an exercise with GD or if you wanted to sharpen the thumb in GD you are shit out of luck unless you are willing to jump through major hoops...
__________________
Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000 Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager ![]() Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
Quote:
I have not used imagemagick much. If I remember right, Dravyk's ContentGod used imagemagick for some of it's image manipulation. I am thinking it used imagemagick for image manipulation and ffmpeg for the video stuff. So, I think I touched it a little bit way back when. I was the last developer on ContentGod before Drav died ![]() So, I might go play with that some. I don't need image manipulation for most of my projects, it is just something I like to play with. Thanks for your opinion, as always. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
Just an aside.
Using this code, the other day I batch processed around 74k images into optimized (1000 on the long side) and thumbs (350 on the long side) and did watermarks on all. Most of the original images were originally around 5k on the long side. About 18GB of files were produced, probably around 85GB original files. The programs ran fine. No time outs. I actually ran 3 instances of it from the command line at the same time. It did take a long time but I just kicked it off and walked away. So, it saved me a bunch of effort. I have another version of it that I use for renaming files using the folder name of the set to make the file names. Original (older content so just 768 X 1024 193kb): ![]() Optimized (750 X 1000 75kb): ![]() Thumb (262 X 350 14kb): ![]() .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 |
this & that
Industry Role:
Join Date: May 2005
Location: Beer City
Posts: 5,303
|
Nice cooter
![]() ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,055
|
![]() Yes, she is very nice. That is Samantha from the old Phoenix/Focus Adult catalog. Years and years ago. .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() ![]() |
|||||||
|
|||||||
Bookmarks |
Tags |
images, optimize, class, php, watermarking, optimizing |
Thread Tools | |