-1

I am making a upload form inside PHP. Though I want the form to upload a thumbnail that is exactly, lets say, 100px high and 100px width. The form has to rescale the tumbnail without stretching the image, but by removing a part from the image. I prefer to do this with 100% php and the most simple script. I already have made my upload form and I only want to know how to make this auto-crop system with my tumbnails.

I really hope you can help me out and I want to thank you in advance.

My script already uploads a image with a thumbnail:

if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }

$path= 'images/' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$thumb_path= 'images/thumb_' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$imgsize2= getimagesize( $imgtemp );
$width= $imgsize2[0];
$height= $imgsize2[1];

$maxwidth= 1281;
$maxheight= 721;
$allowed= array( 'image/png', 'image/jpeg', 'image/gif', );

if( in_array( $imgtype, $allowed ) ){

    if( $width < $maxwidth && $height < $maxheight ){

    if( $imgsize < 5242880 ){

        if( $width == $height ){ $case=1; }
        if( $width > $height ){ $case=2; }
        if( $width < $height ){ $case=3; }

        switch( $case ){

            case 1:

            $newwidth= 100;
            $newheight= 100;

            break;

            case 2:

            $newheight= 100;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            case 3:

            $newwidth= 100;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            }

            switch( $imgtype ){

                case 'image/jpeg';

                $img= imagecreatefromjpeg( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagejpeg( $thumb, $thumb_path );  

                break;

                case 'image/png';

                $img= imagecreatefrompng( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagepng( $thumb, $thumb_path ); 

                break;

                case 'image/gif';

                $img= imagecreatefromgif( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagegif( $thumb, $thumb_path ); 

                break;

                } if(empty($errors))  {

        move_uploaded_file( $imgtemp, $path );
        $upimage = "Image is successfully uploaded.";
                }
        } else{
        $errors[9] = "The image you just uploaded does not meet the requirements. Your picture is too large. ";
        }

        } else{
        $errors[10] = "The image you just uploaded does not meet the requirements. It is a forbidden extension.";
        }

    } else{
        $errors[11] = "The image you just uploaded does not meet the requirements. It is a forbidden extension. Type: $imgtype,  $image, $imgsize, $imgtemp, $name";
        }
if(empty($errors))  {
move_uploaded_file( $imgtemp, $path );
}
     }
Tom Groot
  • 1,160
  • 1
  • 9
  • 26
  • 1
    have you really not tried a google search? http://www.google.com/search?q=php+crop+image – user428517 May 20 '13 at 16:48
  • I did but it's either too difficult or it's not exactly what I mean. But thanks for your help. – Tom Groot May 20 '13 at 16:49
  • what part of it is difficult? this site is for asking questions about something you're stuck on, not asking for complete solutions to a problem you haven't even attempted to solve yet. – user428517 May 20 '13 at 16:53
  • sgroves, what you say is not true. Why do you get the impression that I didn't do my research? The question is how I can crop an image without stretching it inside a PHP upload form. My script already makes a thumbnail but I want it to upload a thumbnail with an exact width and height – Tom Groot May 20 '13 at 16:57
  • possible duplicate? http://stackoverflow.com/questions/3255773/ – crackmigg May 20 '13 at 17:00
  • Can you than point out to me where I can find the solution to my problem? Because I really can't find it. – Tom Groot May 20 '13 at 17:01

1 Answers1

0

I believe you need to set the x and y coordinates to crop an image. Try something like this. You might have to play with the top and left numbers until you get it where you want. Take note of the imagecopyresized line. Also here is a similar post with an identical answer PHP - cropping image with imagecopyresampled()? You need to set the x and y parameters as noted below. BTW the answer was really easy to find. SO even provides related questions on the right hand side of the page where I found that other example for you. Cheers.

          switch( $case ){

        case 1:
        $top = 50;
        $left = 50;   
        $newwidth= 100;
        $newheight= 100;

        break;

        case 2:

        $newheight= 100;
        $ratio= $newheight / $height;
        $newwidth= round( $width * $ratio );

        break;

        case 3:

        $newwidth= 100;
        $ratio= $newwidth / $width;
        $newheight= $height * $ratio;

        break;

        }

        switch( $imgtype ){

            case 'image/jpeg';

            $img= imagecreatefromjpeg( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,$left,$top, $newwidth, $newheight, $width, $height );
            imagejpeg( $thumb, $thumb_path );  

            break;

            case 'image/png';

            $img= imagecreatefrompng( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
            imagepng( $thumb, $thumb_path ); 

            break;

            case 'image/gif';

            $img= imagecreatefromgif( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
            imagegif( $thumb, $thumb_path ); 

            break;
Community
  • 1
  • 1
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Thank you very much! it really helped me. I am wondering why some can't understand my question. I really apreciate your understanding. – Tom Groot Jun 01 '13 at 13:06
  • No problem just glad I could help. It WAS valid and you even provided working code that just needed a **small** tweak. Just because no one seemed to understand your question doesn't mean it's not a real question. I know it's irritating sometimes. Anyway, I've worked a lot with uploading images on my site so I knew what your were talking about. Good luck. – Panama Jack Jun 01 '13 at 21:20