why make extra work if everything can be done automatically?
I've glued together this piece of code in php, it checks for picture dimensions and filesize. If it is too big, the script resizes the pic. Uses PHP 5 and GD2 library.
It is part of a user gallery admin script I am writing for a community site.
It just needs to be tweaked a bit (easy - just edit parameters) for use on your server.
$ppath = "xyz"; // the path where the pics are stored
/*** rebuild thumbs and resize fulls ***/
$config['fulls'] = $ppath."/";
$config['size'] = 100;
$config['imagequality'] = 80;
$imagelist = GetFileList($config['fulls']);
for($i=0; $i
//detect pix which are either too wide, too high or too heavy
$filetocheck = $config[fulls].$imagelist[$i];
$filetocheckweight = filesize($filetocheck);
$filetochecksize = getimagesize($filetocheck);
$filetocheckwidth = $filetochecksize[0];
$filetocheckheight = $filetochecksize[1];
if(filesize($filetocheck)>120000 || $filetocheckwidth>750 || $filetocheckheight>750)
{
ResizeImageUsingGD($filetocheck, $filetocheck, 750);
}
}
#-#############################################
# desc: GetFileList
# param: [optional] directory to look through
# returns: array with list of images
function GetFileList($dirname="."){
global $config;
$list = array();
if ($handle = opendir($dirname)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.(jpe?g|gif|png)$/i",$file)) {
$list[] = $file;
}
}
closedir($handle);
}
rsort($list);
return $list;
}#-#GetFileList()
#-#############################################
# desc: reads binary image file
# param: ($filename) filename of image to create ($type) int of type. 1=gif,2=jpeg,3=png
# returns: binary img
function ReadImageFromFile($filename, $type) {
$imagetypes = ImageTypes();
switch ($type) {
case 1 :
if ($imagetypes & IMG_GIF){
return $im = ImageCreateFromGIF($filename);
}
break;
case 2 :
if ($imagetypes & IMG_JPEG){
return ImageCreateFromJPEG($filename);
}
break;
case 3 :
if ($imagetypes & IMG_PNG){
return ImageCreateFromPNG($filename);
}
break;
default:
return 0;
}
}#-#ReadImageFromFile()
#-#############################################
# desc: writes binary image to file
# returns: binary img
function WriteImageToFile($im, $filename, $type) {
global $config;
switch ($type) {
case 1 :
return ImageGIF($im, $filename);
case 2 :
return ImageJpeg($im, $filename, $config['imagequality']);
case 3 :
return ImagePNG($im, $filename);
default:
return false;
}
}#-#WriteImageToFile()
#-#############################################
# desc: resizes image if GD was used
# param: ($image) image reference of full size img to use ($newimage) what to save thumbnail as ($size) max width or height to resize to
# returns: (bool) if image was created
function ResizeImageUsingGD($image, $newimage, $size) {
global $config;
list ($width,$height,$type) = GetImageSize($image);
if($im = ReadImageFromFile($image,$type)){
//if image is smaller than the $size, make it actual $size
if($height < $size && $width < $size){
$newheight=$height;
$newwidth=$width;
}
//if image height is larger, height=$size, then calc width
else if($height > $width){
$newheight=$size;
$newwidth=($width / ($height/$size));//cast the resized width as int
}
//if image width is larger, width=$size, then calc width
else{
$newwidth=$size;
$newheight=($height / ($width/$size));//cast the resized height as int
}
if(!$config[universal]){
$im2=ImageCreateTrueColor($newwidth,$newheight );
ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$new height,$width,$height);
}else{
$im2=ImageCreateTrueColor($size,$size);
$background = imagecolorallocate($im2, $config[borderR], $config[borderG], $config[borderB]);
imagefilledrectangle($im2, 0, 0, $size - 1, $size - 1, $background);
if($newwidth==$size){
ImageCopyResampled($im2,$im,0,(($size-$newheight)/2),0,0,$newwidth,$newheight,$width,$height);
}else if($newheight==$size){
ImageCopyResampled($im2,$im,(($size-$newwidth)/2),0,0,0,$newwidth,$newheight,$width,$height);
}
}
if(WriteImageToFile($im2,$newimage,$type)){
return true;
}
}
return false;
}#-#ResizeImageUsingGD()
/*** end rebuild thumbs and resize fulls ***/
?>
I've glued together this piece of code in php, it checks for picture dimensions and filesize. If it is too big, the script resizes the pic. Uses PHP 5 and GD2 library.
It is part of a user gallery admin script I am writing for a community site.
It just needs to be tweaked a bit (easy - just edit parameters) for use on your server.
$ppath = "xyz"; // the path where the pics are stored
/*** rebuild thumbs and resize fulls ***/
$config['fulls'] = $ppath."/";
$config['size'] = 100;
$config['imagequality'] = 80;
$imagelist = GetFileList($config['fulls']);
for($i=0; $i
//detect pix which are either too wide, too high or too heavy
$filetocheck = $config[fulls].$imagelist[$i];
$filetocheckweight = filesize($filetocheck);
$filetochecksize = getimagesize($filetocheck);
$filetocheckwidth = $filetochecksize[0];
$filetocheckheight = $filetochecksize[1];
if(filesize($filetocheck)>120000 || $filetocheckwidth>750 || $filetocheckheight>750)
{
ResizeImageUsingGD($filetocheck, $filetocheck, 750);
}
}
#-#############################################
# desc: GetFileList
# param: [optional] directory to look through
# returns: array with list of images
function GetFileList($dirname="."){
global $config;
$list = array();
if ($handle = opendir($dirname)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.(jpe?g|gif|png)$/i",$file)) {
$list[] = $file;
}
}
closedir($handle);
}
rsort($list);
return $list;
}#-#GetFileList()
#-#############################################
# desc: reads binary image file
# param: ($filename) filename of image to create ($type) int of type. 1=gif,2=jpeg,3=png
# returns: binary img
function ReadImageFromFile($filename, $type) {
$imagetypes = ImageTypes();
switch ($type) {
case 1 :
if ($imagetypes & IMG_GIF){
return $im = ImageCreateFromGIF($filename);
}
break;
case 2 :
if ($imagetypes & IMG_JPEG){
return ImageCreateFromJPEG($filename);
}
break;
case 3 :
if ($imagetypes & IMG_PNG){
return ImageCreateFromPNG($filename);
}
break;
default:
return 0;
}
}#-#ReadImageFromFile()
#-#############################################
# desc: writes binary image to file
# returns: binary img
function WriteImageToFile($im, $filename, $type) {
global $config;
switch ($type) {
case 1 :
return ImageGIF($im, $filename);
case 2 :
return ImageJpeg($im, $filename, $config['imagequality']);
case 3 :
return ImagePNG($im, $filename);
default:
return false;
}
}#-#WriteImageToFile()
#-#############################################
# desc: resizes image if GD was used
# param: ($image) image reference of full size img to use ($newimage) what to save thumbnail as ($size) max width or height to resize to
# returns: (bool) if image was created
function ResizeImageUsingGD($image, $newimage, $size) {
global $config;
list ($width,$height,$type) = GetImageSize($image);
if($im = ReadImageFromFile($image,$type)){
//if image is smaller than the $size, make it actual $size
if($height < $size && $width < $size){
$newheight=$height;
$newwidth=$width;
}
//if image height is larger, height=$size, then calc width
else if($height > $width){
$newheight=$size;
$newwidth=($width / ($height/$size));//cast the resized width as int
}
//if image width is larger, width=$size, then calc width
else{
$newwidth=$size;
$newheight=($height / ($width/$size));//cast the resized height as int
}
if(!$config[universal]){
$im2=ImageCreateTrueColor($newwidth,$newheight );
ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$new height,$width,$height);
}else{
$im2=ImageCreateTrueColor($size,$size);
$background = imagecolorallocate($im2, $config[borderR], $config[borderG], $config[borderB]);
imagefilledrectangle($im2, 0, 0, $size - 1, $size - 1, $background);
if($newwidth==$size){
ImageCopyResampled($im2,$im,0,(($size-$newheight)/2),0,0,$newwidth,$newheight,$width,$height);
}else if($newheight==$size){
ImageCopyResampled($im2,$im,(($size-$newwidth)/2),0,0,0,$newwidth,$newheight,$width,$height);
}
}
if(WriteImageToFile($im2,$newimage,$type)){
return true;
}
}
return false;
}#-#ResizeImageUsingGD()
/*** end rebuild thumbs and resize fulls ***/
?>
Comment