There is a form through which people upload images to the site. The script checks if it is a picture or not, if it is a picture, it saves it and outputs it to the browser. How to make so that when loading through a form the picture is cropped to the size of 100 by 100 pixels? So far, Google hasn’t found anything in Google.

Here is the actual PHP code:

if(isset($_FILES['filename']['name'])) { $name = $_FILES['filename']['name']; $typeok = TRUE; #### #$name = strtolower(ereg_replace("[^A-Za-z0-9.]", "", $name); //Скрипт перестает работать, если раскомментировать эту строчку(( switch($_FILES['filename']['type']) { case 'image/jpeg': $ext = 'jpg'; break; case 'image/pjpeg': $ext = 'jpg'; break; case 'image/gif': $ext = 'gif'; break; case 'image/png': $ext = 'png'; break; case 'image/tiff': $ext = 'tif'; break; default: $typeok = FALSE; break; } if($ext) { $n = strtolower("$name"); move_uploaded_file($_FILES['filename']['tmp_name'], "images/users/$n"); echo("<div class=\"alert alert-success\">"); echo("<p>Загружено изображение $name под именем $n: </p><br>"); echo("<img src='images/users/$n'>"); echo("</div>"); } else { echo("<div class=\"alert alert-error\">"); echo ("<p>'$name' - неприемлимый файл изображения</p>"); echo("</div>"); } } else { echo("<div class=\"alert alert-error\">"); echo ("<p>Загрузки изображения не произошло!</p>"); echo("</div>"); } 
  • one
    $ name = strtolower (ereg_replace ("[^ A-Za-z0-9.]", "", $ name); ---------- $ name = strtolower (ereg_replace ("[^ A- Za-z0-9.] "," ", $ Name)); - activist
  • one
    The procedure is based on the example of JPEG: imagecreatetruecolor, imagecreatefromjpeg, imagecopyresampled, imagejpeg Create an empty image with dimensions 100x100, load an existing image, copy the necessary part of the image into an empty one, save it. - activist
  • Thanks for the tips, now try to implement! - spoilt

2 answers 2

Here is an interesting plugin: cropping pictures

    You can like this:

     $crop_x = ...; $crop_y = ...; $crop_width = ...; $crop_height = ...; $image_p = imagecreatetruecolor($crop_width, $crop_height); $image = imagecreatefromjpeg(...); imagecopyresampled($image_p, $image, 0, 0, $crop_x, $crop_y, $crop_width, $crop_height, $crop_width, $crop_height); $img_data = tempnam("/tmp","crop"); //сохраняем тут по имени $img_data imagejpeg($image_p, $img_data); imagedestroy($image_p); imagedestroy($image); unlink($img_data);