Well, guys, the question was how to cut an image in the center by uploading it. I researched on the Internet and ended up changing my mind and using JCROP, but the question itself had not been answered, so I searched the subject on the Internet and ended up finding a script that solved my question. It’s not my script, but I did a little minor editing to better fit my initial goal.
Here is the script:
<form action="" method="post" enctype="multipart/form-data">
<input name="img" type="file" />
<input type="submit" name="cadastrar" />
</form>
<?php
if(isset($_POST['cadastrar'])){
$img = $_FILES['img'];
$name =$img['name'];
$tmp =$img['tmp_name'];
$ext =end(explode('.',$name));
$pasta ='NOMEDAPASTA/'; //Pasta onde a imagem será salva
$permiti =array('jpg', 'jpeg', 'png');
$name = uniqid().'.'.$ext; $uid = uniqid();
$upload = move_uploaded_file($tmp, $pasta.'/'.$name);}; //Faz o upload da imagem para o servidor
if($upload){
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 60){
$imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
//resize and crop image by center
switch($mime){
case 'image/gif':
$image_create = "imagecreatefromgif";
$image = "imagegif";
break;
//resize and crop image by center
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 6;
break;
//resize and crop image by center
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 60;
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
if($width_new > $width){
$h_point = (($height - $height_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
}else{
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
//Tamanho da Imagem final
resize_crop_image(300, 300, $pasta.'/'.$name, $pasta.'/'.$name);}
?>
I don’t know if this automatic is the best... Not all images have the focus in the center.
– ptkato
I searched the internet and using jcrop, so when you upload the user can choose which point of the image he wants to display. That way I solve both problems. One on the image if of different size and the other on the focus of the image, so the user himself decided which part of the image is the best
– ivan veloso