php - A script to rewrite all images that are a certain width in a different size? -
i using gd library php.
basically, i've made design change , need resize whole bunch of images width.
ie 876px wide needs 828px.
is there way loop through jpg files in directory, check width dimension, , if equal x grab existing file name, rescale down same name?
just need use imagecopyresampled() in loop.
$path = "/my/path/to/jpgs"; $targetwidth = 876; $newwidth = 828; $imagequality = 95; if (is_dir($path)) { $dhandle = opendir($path); if ($dhandle) { while (($cfilename = readdir($dhandle)) !== false) { if (!preg_match("/\.jpg$/", $cfilename)) { continue; } $cimagepath = $path . $cfilename; list ($cwidth, $cheight) = getimagesize($cimagepath); if ($cwidth == $targetwidth) { $cimage = imagecreatefromjpeg($cimagepath); if ($cimage === false) { echo "error reading: " . $cimagepath . "\n"; continue; } $cnewheight = round($cheight * ($newwidth / $cwidth)); $cnewimage = imagecreatetruecolor($newwidth, $cnewheight); imagecopyresampled($cnewimage, $cimage, 0, 0, 0, 0, $newwidth, $cnewheight, $cwidth, $cheight); if (imagejpeg($cnewimage, $cimagepath, $imagequality) === false) { echo "error writing: " . $cimagepath . "\n"; continue; } } } closedir($dhandle); } }
Comments
Post a Comment