perezf Posted March 23, 2008 Share Posted March 23, 2008 Hey everyone, How can i resize every image in a directory. What im going to do is have a cron job that runs my file. The file will resize every image in a given directory to the width 100px x 100px The reason im doing this is because i dont have control of the images that are uploaded via ftp can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/97496-image-resize/ Share on other sites More sharing options...
MadTechie Posted March 23, 2008 Share Posted March 23, 2008 Try this, untested but should be ok, your need to mod it for PNG & GIF, as i pulled the CreateThumb function from an old project and tweeked it a little <?php $FTP_upload = dirname(__FILE__)."/FTP/";//From Dir $WWW_Images = dirname(__FILE__)."/images/"; //To Dir if(!$dh = @opendir($FTP_upload)) return; while (false !== ($obj = readdir($dh))) { if($obj=='.' || $obj=='..') continue; CreateThumb($FTP_upload.'/'.$obj, $WWW_Images.'/'.$obj,100,100) } } closedir($dh); function CreateThumb($name,$filename,$new_w,$new_h, $Qty=100) { ini_set("memory_limit","32M"); if (preg_match('/(?:\.jpg|\.jpeg)$/i', $name)) { $src_img=imagecreatefromjpeg($name); } if( $src_img === false || $src_img === NULL) { echo "Failed:<br>"; return false; } $old_x=imageSX($src_img); $old_y=imageSY($src_img); //dont resize up. if( ($new_w > $old_x) && ($new_h > $old_y)) { $thumb_w=$old_x; $thumb_h=$old_y; }else{ if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } } $dst_img=imagecreatetruecolor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); imagejpeg($dst_img,$filename, $Qty); imagedestroy($dst_img); imagedestroy($src_img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97496-image-resize/#findComment-498868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.