Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help cutomizing script

Status
Not open for further replies.

pugs421

Technical User
Joined
Nov 25, 2002
Messages
114
Location
US
Newbie*
I made changes to this original script to make it do what I want it to, except for one thing. If the user tries to upload a large dimension pisture it will resize it down to the given specifications(thats good). But if the user uploads a small dimension picture it will enlarge it to that specification(bad).

I guess I need to add some kind of conditional statement that says if the image width is less than 250 dont resize.

Thanks

_________________________________________________________
<FORM action=&quot;imgresizer41.php&quot; encType=&quot;multipart/form-data&quot; method=&quot;post&quot; name=&quot;sendform&quot;>
<INPUT name=MAX_FILE_SIZE type=hidden value=10000000>
<INPUT maxLength=128 name=image type=file ACCEPT=&quot;image/gif, image/jpeg, image/jpg&quot;>
<br>
Name:<INPUT maxLength=128 name=&quot;img_name&quot; type=&quot;text&quot;>
<br>
<INPUT name=Submit type=submit value=Submit>
<INPUT name=Reset type=reset value=Reset>
</FORM>
<p>
<b>If you need some custom programming you can order it online <a href=&quot;</p>
<?PHP
#############################################################################################
$IMG_ORG_HEIGHT = &quot;*&quot;; #Width of original image stored on server
$IMG_ORG_WIDTH = &quot;250&quot;; #Height of original image stored on server
#set to &quot;*&quot; to prevent resizing on the dimension
#you will not lost X to Y ratio

$IMG_HEIGHT = 108; # Accepted height of resized image
$IMG_WIDTH = 108; # Accepted width of resized image
# read about &quot;*&quot; above

$IMG_ROOT = &quot;./img4&quot;; # Relative path to folder where uploaded images will be stored; no ending slash!
# Like this: $IMG_ROOT = &quot;./img&quot;;
# Remeber to set proper attributes to that folder. 777 will work :)
$use_imagecreatetruecolor = false; // these flags enble ImageCreateTrueColor(); ImageCopyResampled();
$use_imagecopyresampled = false;// I cant test them coz my host doesn't allow these...
$JPG_QUALITY = 90; // output jpeg quality

#############################################################################################
//error_reporting(53); // dont need ur warnings!
if(!$HTTP_POST_FILES [&quot;image&quot;][&quot;tmp_name&quot;] || $HTTP_POST_FILES [&quot;image&quot;][&quot;tmp_name&quot;] ==&quot;none&quot;) die(&quot;<br>&quot;);

if( ! $f_org = resizer_main(&quot;image&quot;,&quot;&quot;,$IMG_ORG_WIDTH,$IMG_ORG_HEIGHT))die(&quot;<br><font color=\&quot;red\&quot;><b>No Image received...</b></font><br>&quot;);
if( ! $f_res = resizer_main(&quot;image&quot;,&quot;res_&quot;,$IMG_WIDTH,$IMG_HEIGHT))die(&quot;<br><font color=\&quot;red\&quot;><b>Not resized :( </b></font><br>&quot;);


$sz_org =getimagesize( &quot;$IMG_ROOT/$f_org&quot; );
$sz_res =getimagesize( &quot;$IMG_ROOT/$f_res&quot; );
$fs_org= filesize(&quot;$IMG_ROOT/$f_org&quot; );
$fs_res= filesize(&quot;$IMG_ROOT/$f_res&quot; );



#############################################################################################

function resizer_main($image, $dest_file_prefix,$w, $h){
//image_name = uploaded image. Name or your file field in your form.
//w,h - width and height to fit image in
global $use_imagecreatetruecolor, $use_imagecopyresampled, $IMG_ROOT, $JPG_QUALITY, $HTTP_POST_FILES;
$image_name = $HTTP_POST_FILES [$image][&quot;name&quot;];
$image = $HTTP_POST_FILES [$image][&quot;tmp_name&quot;];

if(trim($image) == &quot;&quot; || trim($image) ==&quot;none&quot;) return false;

$arr_img = image_from_upload($image);
if( $arr_img[&quot;w&quot;] != $w && $arr_img[&quot;h&quot;] != $h){
$wh = get_sizes($arr_img[&quot;w&quot;], $arr_img[&quot;h&quot;], $w, $h);
$img_res = img_get_resized(
$arr_img[&quot;img&quot;],
$arr_img[&quot;w&quot;], $arr_img[&quot;h&quot;],
$wh[&quot;w&quot;], $wh[&quot;h&quot;],
$use_imagecreatetruecolor,
$use_imagecopyresampled);
} else {
//wow it is exactly like needed!!!
$img_res = $arr_img[&quot;img&quot;];
}
$file_name = make_filename($image_name);

ImageJPEG($img_res,&quot;$IMG_ROOT/$dest_file_prefix$file_name&quot;, $JPG_QUALITY);

return &quot;$dest_file_prefix$file_name&quot;;
}

function image_from_upload($uploaded_file){

$img_sz = getimagesize( $uploaded_file ); ## returns array with some properties like dimensions and type;
####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
switch( $img_sz[2] ){
case 1:
$img_type = &quot;GIF&quot;;
$img = ImageCreateFromGif($uploaded_file);
break;
case 2:
$img = ImageCreateFromJpeg($uploaded_file);
$img_type = &quot;JPG&quot;;
break;
case 3:
$img = ImageCreateFromPng($uploaded_file);
$img_type = &quot;PNG&quot;;
break;
case 4:
$img = ImageCreateFromSwf($uploaded_file);
$img_type = &quot;SWF&quot;;
break;
default: die(&quot;<br><font color=\&quot;red\&quot;><b>Sorry, this image type is not supported yet.</b></font><br>&quot;);
}//case
return array(&quot;img&quot;=>$img, &quot;w&quot;=>$img_sz[0], &quot;h&quot;=>$img_sz[1], &quot;type&quot;=>$img_sz[2], &quot;html&quot;=>$img_sz[3]);

}


function get_sizes($src_w, $src_h, $dst_w,$dst_h ){
//src_w ,src_h-- start width and height
//dst_w ,dst_h-- end width and height
//return array w=>new width h=>new height mlt => multiplier
//the function tries to shrink or enalrge src_w,h in such a way to best fit them into dst_w,h
//keeping x to y ratio unchanged
//dst_w or/and dst_h can be &quot;*&quot; in this means that we dont care about that dimension
//for example if dst_w=&quot;*&quot; then we will try to resize by height not caring about width
//(but resizing width in such a way to keep the xy ratio)
//if both = &quot;*&quot; we dont resize at all.
#### Calculate multipliers
$mlt_w = $dst_w / $src_w;
$mlt_h = $dst_h / $src_h;

$mlt = $mlt_w < $mlt_h ? $mlt_w:$mlt_h;
if($dst_w == &quot;*&quot;) $mlt = $mlt_h;
if($dst_h == &quot;*&quot;) $mlt = $mlt_w;
if($dst_w == &quot;*&quot; && $dst_h == &quot;*&quot;) $mlt=1;

#### Calculate new dimensions
$img_new_w = round($src_w * $mlt);
$img_new_h = round($src_h * $mlt);
return array(&quot;w&quot; => $img_new_w, &quot;h&quot; => $img_new_h, &quot;mlt_w&quot;=>$mlt_w, &quot;mlt_h&quot;=>$mlt_h, &quot;mlt&quot;=>$mlt);
}

function img_get_resized($img_original,$img_w,$img_h,$img_new_w,$img_new_h,$use_imagecreatetruecolor=false, $use_imagecopyresampled=false){

//$img_original, -- image to be resized
//$img_w, -- its width
//$img_h, -- its height
//$img_new_w, -- resized width
//$img_new_h -- height
//$use_imagecreatetruecolor, $use_imagecopyresampled allow use of these function
//if they exist on the server

if( $use_imagecreatetruecolor && function_exists(&quot;imagecreatetruecolor&quot;)){
// echo(&quot;Using ImageCreateTruecolor (better quality)<br>&quot;);
$img_resized = imagecreatetruecolor($img_new_w,$img_new_h) or die(&quot;<br><font color=\&quot;red\&quot;><b>Failed to create destination image.</b></font><br>&quot;);
} else {
// echo(&quot;Using ImageCreate (usual quality)<br>&quot;);
$img_resized = imagecreate($img_new_w,$img_new_h) or die(&quot;<br><font color=\&quot;red\&quot;><b>Failed to create destination image.</b></font><br>&quot;);

}
if($use_imagecopyresampled && function_exists(&quot;imagecopyresampled&quot;)){
// echo(&quot;Using ImageCopyResampled (better quality)<br>&quot;);
imagecopyresampled($img_resized, $img_original, 0, 0, 0, 0,$img_new_w, $img_new_h, $img_w,$img_h) or die(&quot;<br><font color=\&quot;red\&quot;><b>Failed to resize @ ImageCopyResampled()</b></font><br>&quot;);

}else{
// echo(&quot;Using ImageCopyResized (usual quality)<br>&quot;);
imagecopyresized($img_resized, $img_original, 0, 0, 0, 0,$img_new_w, $img_new_h, $img_w,$img_h) or die(&quot;<br><font color=\&quot;red\&quot;><b>Failed to resize @ ImageCopyResized()</b></font><br>&quot;);
}
return $img_resized;
}

function make_filename($image_name){
global $HTTP_POST_VARS;
## creates unique name, here I assume that it will never happen that in same second
## two files with same name on user's site will be uploaded. However you can use your
## ways to generate unique name. Function unqueid() for example.

$file_name = time().&quot;_$image_name&quot;;
#kick the original extension
$pos = strrpos($file_name, '.');
//personally I think jpeg rulez so I hardoce its extension here
$file_name = substr($file_name, 0,$pos).&quot;.jpg&quot;;




$file_name = $HTTP_POST_VARS[&quot;img_name&quot;].&quot;.jpg&quot;;

return $file_name;
}

?>
 
Would anyone be able to point me in the right direction on how to implement the getimagesize() in this case. I read up on it but I cant figure out how to make it work (or where I'm actually supposed to use it for that matter). I'd really appreciate it.
 
OK, see this main function:
Code:
function resizer_main($image, $dest_file_prefix,$w, $h){
//image_name = uploaded image. Name or your file field in your form.
//w,h - width and height to fit image in
global $use_imagecreatetruecolor, $use_imagecopyresampled, $IMG_ROOT, $JPG_QUALITY, $HTTP_POST_FILES;
$image_name = $HTTP_POST_FILES [$image][&quot;name&quot;];
$image = $HTTP_POST_FILES [$image][&quot;tmp_name&quot;];

if(trim($image) == &quot;&quot; || trim($image) ==&quot;none&quot;) return false;

    $arr_img = image_from_upload($image);
    [b]if( $arr_img[&quot;w&quot;] != $w && $arr_img[&quot;h&quot;] != $h){[/b]
        $wh    = get_sizes($arr_img[&quot;w&quot;], $arr_img[&quot;h&quot;], $w, $h);
        $img_res = img_get_resized(
            $arr_img[&quot;img&quot;], 
            $arr_img[&quot;w&quot;],    $arr_img[&quot;h&quot;], 
            $wh[&quot;w&quot;],        $wh[&quot;h&quot;], 
            $use_imagecreatetruecolor, 
            $use_imagecopyresampled);
    } else {
            //wow it is exactly like needed!!!
            $img_res = $arr_img[&quot;img&quot;];
    }
    $file_name = make_filename($image_name);

    ImageJPEG($img_res,&quot;$IMG_ROOT/$dest_file_prefix$file_name&quot;, $JPG_QUALITY);

    return &quot;$dest_file_prefix$file_name&quot;;
}
I highlighted this row:
Code:
if( $arr_img[&quot;w&quot;] != $w && $arr_img[&quot;h&quot;] != $h){
This row causes the image resizing when the image size doesn't equals to default values. To force the function to resize pictures only when the pictures are larger, you simply write the line like this:
Code:
if( $arr_img[&quot;w&quot;] > $w && $arr_img[&quot;h&quot;] > $h){

I didn't verified the correct functionality, but I thing this is exactly what you want. If this will not work, I will try to study the code deeper if there isn't another condition.

Hope this helps!
 
Oh, the highlight with &#91;b&#93; tags doesn't work within the &#91;code&#93; tags, so the line isn't highlighted but only begins with the &#91;b&#93; tag and ends with &#91;/b&#93; But I thing you will find the right line anyway ;D
 
You may encounter some problems eg. when the width is larger and the height is narrower. Then code above will work only if the height and width are everytime either wider than default values or narrower than default values. To fix this problem you can write this:
Code:
if( $arr_img[&quot;w&quot;] > $w || $arr_img[&quot;h&quot;] > $h){
which may help you a bit.

But if the width is smaller and the height is bigger, there will be modified both width and height. But this is probably necessary. I don't have much knowledge about picture handling algorithms but I thing that both height and width must be modified to keep the correct picture look.
 
Thanks a lot. This wound up working great, but I had to remove the '*' (wildcard) from the maximum allowed hieght in order to make it work. Youve made my day. thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top