Note that there are some explanatory texts on larger screens.

plurals
  1. POCrop image without stretching it, inside PHP
    primarykey
    data
    text
    <p>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.</p> <p>I really hope you can help me out and I want to thank you in advance.</p> <p>My script already uploads a image with a thumbnail:</p> <pre><code>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 &lt; $maxwidth &amp;&amp; $height &lt; $maxheight ){ if( $imgsize &lt; 5242880 ){ if( $width == $height ){ $case=1; } if( $width &gt; $height ){ $case=2; } if( $width &lt; $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 ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload