Page 1 of 1
resize files Help if smaller then don't resize
Posted: 2010-07-07T12:26:25-07:00
by winracer
here is the code I am using below,
what I want if the files is smaller then
$max_width = "400";
$max_height = "400";
don't resize and if lrger resize to
$max_width = "400";
$max_height = "400";
does anyone know how I would do this.
Code: Select all
<?php
// Setup the maximium width and height for the finished image code came from http://www.rubblewebs.co.uk/imagemagick/codes/auto_thumb.php
$max_width = "400";
$max_height = "400";
// Directory containing the images - need the trailing slash /
$read = 'upload/';
$read1 = 'thumbs/';
// Open the directory and create an array of the images
$myDirectory = opendir( $read );
while( $entryName = readdir( $myDirectory ))
{
// This will only select jpg images from the folder
foreach (glob($dir."{*.jpg,*.png,*.gif}",GLOB_BRACE ) as $filename) {
$dirArray[]=$entryName;
}
}
closedir( $myDirectory );
// Count the number of images
$indexCount = count($dirArray);
for ( $i=0; $i<$indexCount; $i++ )
{
$name = $read.$dirArray[$i];
// Save resized images as folder/thumb_original name
//$new_name = $read1."thumb_".$dirArray[$i];
$new_name = $read1.$dirArray[$i];
// Get the image size for use in the code
$size=GetImageSize( $name );
exec("convert -size {$size[0]}x{$size[1]} $name -thumbnail $max_widthx$max_height $new_name");
}
?>
Re: resize files Help if smaller then don't resize
Posted: 2010-07-07T14:03:35-07:00
by Bonzo
I would use php to check the size as ImageMagick reads the image in checks the size and even if its not resized IM saves it which adds jpg compression.
These are the ImageMagick settings but I can not remember which one to use:
http://www.imagemagick.org/script/comma ... p#geometry
Code: Select all
// Get the image size for use in the code
$size=GetImageSize( $name );
if( ( $size[0] > 400 ) or ( $size[1] > 400 ) {
exec("convert -size {$size[0]}x{$size[1]} $name -thumbnail $max_widthx$max_height $new_name");
}
Re: resize files Help if smaller then don't resize
Posted: 2010-07-07T15:06:59-07:00
by Drarakel
If you add further operations to your ImageMagick command, then the usage of the '>' symbol ("-thumbnail $max_widthx$max_height\>") would be enough, as this does
shrink only larger images.
But for your current commands, Bonzo's solution is probably better (copy directly the input file if it's not larger).
Re: resize files Help if smaller then don't resize
Posted: 2010-07-08T05:49:38-07:00
by winracer
thanks for your help. I will give it a try and let you know. again thanks
Re: resize files Help if smaller then don't resize
Posted: 2010-07-08T10:08:29-07:00
by winracer
thanks for your help works great.
Made a few changes....
Code: Select all
<?php
$convert = '/usr/bin/convert';
$identify = '/usr/bin/identify';
// Setup the maximium width and height for the finished image http://www.rubblewebs.co.uk/imagemagick/codes/auto_thumb.php
$max_width = "400";
$max_height = "400";
$read = 'upload/';
$read1 = 'thumbs/';
// Open the directory and create an array of the images
$myDirectory = opendir( $read );
while($entryName = readdir($myDirectory))
{
// This will only select jpg images from the folder
$dirArray = array();
foreach (glob($dir."{*.jpg,*.png,*.gif}",GLOB_BRACE ) as $filename) {
$dirArray = $entryName;
}
echo $entryName."\n<br />";
// Directory containing the images - need the trailing slash /
$input = "upload/$entryName";
$output = "thumbs/$entryName";
$size = GetImageSize($input);
exec("convert -size {$size[0]}x{$size[1]} $input -thumbnail $max_widthx$max_height\> $output");
$totalimages = array( $entryName );
$totalElements = count( $totalimages );
for ( $i=0; $i < $totalElements; $i++ ) {
// echo $totalimages[$i]."<br />";
$C++;
// echo $C."<br />";
//if ( $i < $totalElements -1 ) echo ", ";
}
}
closedir($myDirectory);
echo $C." Total<br />";
?>
Re: resize files Help if smaller then don't resize
Posted: 2010-07-12T19:10:18-07:00
by anthony
Before finishing, you need to ask one simple question. Is security a concern?
For example you do $entryName = readdir($myDirectory) which means the value comes from the the directory. Can you be certain a malicious user has not put in some weird filename in that directory. For example one that contains quotes, spaces, returns, semi-colons, wildcards, etc etc etc?
Also while you test for the suffix, can you be sure that you couldn't have a sub-directory name also having that same suffix! For example a directory called "some.gif"? You should at least have a 'filetype' test as a minimum so as to ignore special files, like directories, or named pipes.
If it is only a user script no problem. but if the PHP is being used from a web server, then you may need better 'taint' checking of file and directory names. Remember under UNIX/Linux a filename can contain ANY CHARACTER (except NULL and SLASH).
Most IM scripts do not worry about this type of thing as they are not meant for a insecure environment, but PHP is usually meant for web servers which is an 'insecure' environment.
Re: resize files Help if smaller then don't resize
Posted: 2010-07-13T05:32:09-07:00
by winracer
anthony wrote:Before finishing, you need to ask one simple question. Is security a concern?
For example you do $entryName = readdir($myDirectory) which means the value comes from the the directory. Can you be certain a malicious user has not put in some weird filename in that directory. For example one that contains quotes, spaces, returns, semi-colons, wildcards, etc etc etc?
Also while you test for the suffix, can you be sure that you couldn't have a sub-directory name also having that same suffix! For example a directory called "some.gif"? You should at least have a 'filetype' test as a minimum so as to ignore special files, like directories, or named pipes.
If it is only a user script no problem. but if the PHP is being used from a web server, then you may need better 'taint' checking of file and directory names. Remember under UNIX/Linux a filename can contain ANY CHARACTER (except NULL and SLASH).
Most IM scripts do not worry about this type of thing as they are not meant for a insecure environment, but PHP is usually meant for web servers which is an 'insecure' environment.
thanks so I should do somting like
$filetype = jpg,*.png,*.gif;
$dirArray = array();
foreach (glob($dir."{$filetype}",GLOB_BRACE ) as $filename) {
$dirArray = $entryName;
Re: resize files Help if smaller then don't resize
Posted: 2010-07-13T16:25:54-07:00
by anthony
winracer wrote:thanks so I should do somting like
$filetype = jpg,*.png,*.gif;
$dirArray = array();
foreach (glob($dir."{$filetype}",GLOB_BRACE ) as $filename) {
$dirArray = $entryName;
That does not change anything. What you need to do (at a minimum) is file test to see if the filename is a plain_file or directory (or something else) and then either junk the filename or store it for later processing (if recursing in to sub-directories).
I am not certain how that is done in PHP.
Also checking for 'unusual characters' is a good security measure if you can be unsure of the environment the script is being used in.
My list of unusual characters include... ; | ^ & ` > < * ? [ ] \ ' " $ % space tab return newline
Also exclude : if you passing the file name to IM
Alternately exclude all characters except A-Z a-Z 0-9 _ - + = .
You may also want to exclude / from input names if paths are not permitted, or things like . and .. if relative paths they are permitted.
Basically security is a matter of restriction in un-controlled environments. On the other hand restricting users is a bad thing if the script is not directly used in controlled environment (web, daemon and network services)
Re: resize files Help if smaller then don't resize
Posted: 2010-07-13T19:37:05-07:00
by winracer
anthony wrote:winracer wrote:thanks so I should do somting like
$filetype = jpg,*.png,*.gif;
$dirArray = array();
foreach (glob($dir."{$filetype}",GLOB_BRACE ) as $filename) {
$dirArray = $entryName;
That does not change anything. What you need to do (at a minimum) is file test to see if the filename is a plain_file or directory (or something else) and then either junk the filename or store it for later processing (if recursing in to sub-directories).
I am not certain how that is done in PHP.
Also checking for 'unusual characters' is a good security measure if you can be unsure of the environment the script is being used in.
My list of unusual characters include... ; | ^ & ` > < * ? [ ] \ ' " $ % space tab return newline
Also exclude : if you passing the file name to IM
Alternately exclude all characters except A-Z a-Z 0-9 _ - + = .
You may also want to exclude / from input names if paths are not permitted, or things like . and .. if relative paths they are permitted.
Basically security is a matter of restriction in un-controlled environments. On the other hand restricting users is a bad thing if the script is not directly used in controlled environment (web, daemon and network services)
thanks for your help ans I will work on it.