1024programmer PHP PHP cutting problem, please give me some advice, I have been working on it for a day.

PHP cutting problem, please give me some advice, I have been working on it for a day.

I use JCROP to obtain the x, y coordinates, width, and height on the front end, and pass them to uphoto.php for processing. Now everything is normal, but if the width of the picture selected by the front-end user is too large (I use css style to control the maximum width of the picture selected by the user to 680px ), the cropped image is abnormal… I checked some information and it said that it needs to be converted in proportion, but at what proportion? Is the width of the image selected by the user (when greater than 680px) converted to 680?

This is uphoto.php:

$x1 = $_POST["x1"];$x2 = $  _POST["x2"];$y1 = $_POST["y1"];$y2 = $_POST["y2"];$targ_w = ​​$_POST["w"];$targ_h = $_POST["h"]  ;if ($_FILES["mfile"]["error"]){echo "error: " . $_FILES["file"]["error"];}if (!empty($_FILES["mfile"][  "name"])){ //Extract the file domain content name and determine $path = "/upload/user/photo/"; //Upload path//Check whether the folder exists, if not, create it and give it  Highest authority //if(!file_exists($path)){mkdir("$path", 0700);}//File formats allowed to be uploaded $allow_type = array("image/gif","image/pjpeg","  image/jpeg","image/png");//Check whether the uploaded file is of a type that is allowed to be uploaded if(!in_array($_FILES["mfile"]["type"], $allow_type)){echo "error:  The format is wrong, only gif/jpg/png is supported"; exit();}//Detect image size if($_FILES["mfile"]["size"] > 5*1024*1024){echo "error: file  Must not exceed 5M"; exit();}$filetype = $_FILES["mfile"]["type"];if($filetype == "image/jpeg"){$img_type = ".jpg";}if (  $filetype == "image/jpg") {$img_type = ".jpg";}if ($filetype == "image/pjpeg") {$img_type = ".jpg";}if($filetype == "image  /gif"){$img_type = ".gif";}if($filetype == "image/png"){$img_type = ".png";}if($_FILES["mfile"]["name"]  ){$randstr = random_str().date("YmdHis"); //Get the time and assign it to the variable $saveto = $_SERVER['DOCUMENT_ROOT'].$path.$randstr.$img_type; //The full path of the image  $newimg = $randstr.$img_type; //Picture name$flag=1;}//END IFif ($flag){$result = move_uploaded_file($_FILES["mfile"]["tmp_name"], $saveto);  }//Specially note that the first parameter passed to move_uploaded_file here is the temporary file uploaded to the server $src_file = $saveto;}}$type = exif_imagetype($src_file);$support_type = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF)  ;if(!in_array($type, $support_type, true)){echo "error: The current image format is not JPG/PNG/GIF";exit();}//Load imageswitch($type) {case IMAGETYPE_JPEG :$src_img  = imagecreatefromjpeg($src_file);break;case IMAGETYPE_PNG :$src_img = imagecreatefrompng($src_file);break;case IMAGETYPE_GIF :$src_img = imagecreatefromgif($src_file);break;default:echo "Load image error!";exit()  ;}$dst_r = ImageCreateTrueColor($targ_w, $targ_h);list($width_orig, $height_orig) = getimagesize($src_file);//if ($width_orig > 680){//$new_width_orig = 680;//$new_height_orig  = ($height_orig*680)/$width_orig;//}//else {//$new_width_orig = $targ_w;//$new_height_orig = $targ_h;//}//echo $new_width_orig."
"; //echo $new_height_orig."
";//echo "x:".$x1."
";//echo "y:".$y1;//exit();imagecopyresampled( $dst_r, $src_img, 0, 0, $x1, $y1, $targ_w, $targ_h, $targ_w, $targ_h);//imagecopyresampled($dst_r, $src_img, 0, 0, $x1, $y1, $ targ_w, $targ_h, $targ_w, $targ_h);switch($type) {case IMAGETYPE_JPEG :header('Content-type: image/jpeg');imagejpeg($dst_r, null, 100);break;case IMAGETYPE_PNG :header ('Content-type: image/png');imagepng($dst_r, null, 100);break;case IMAGETYPE_GIF :header('Content-type: image/gif');imagegif($dst_r, null, 100); break;default:break;}echo "";exit;

Please give me some advice

Reply to discussion (solution)

Use css style to control the maximum width of the image selected by the user to 680px
It means that the user sees is a dynamically scaled copy
Then when you pass the coordinates of the cropping area, you also need to pass the original size of the image
Just convert it
X= x/680*image width
br />

Use css style to control the maximum width of the image selected by the user to 680px
It means that what the user sees is Dynamically scaled copy
Then when you pass the coordinates of the cropping area, you also need to pass the original size of the image
Just convert it
X= x/680*image width

Thank you, the conversion you did is still abnormal..

list($width_orig, $height_orig) = getimagesize($src_file);
if ($width_orig > 680){
$nx = $x1/680 * $width_orig;
}
else {
$nx = $x1;
}

imagecopyresampled($dst_r, $src_img, 0, 0, $nx, $y1, $targ_w, $targ_h, $targ_w, $targ_h);

Your y direction is not calculated

list($width_orig, $height_orig) = getimagesize($src_file);
if ($width_orig > 680) {
$nx = $x1/680 * $width_orig;
$new_height = $height_orig*680/$width_orig;
$ny = $y1/$new_height * $height_orig;
> }
else {
$nx = $x1;
$ny = $y1;
}
imagecopyrMust exceed 5M”; exit();}$filetype = $_FILES[“mfile”][“type”];if($filetype == “image/jpeg”){$img_type = “.jpg”;}if ( $filetype == “image/jpg”) {$img_type = “.jpg”;}if ($filetype == “image/pjpeg”) {$img_type = “.jpg”;}if($filetype == “image /gif”){$img_type = “.gif”;}if($filetype == “image/png”){$img_type = “.png”;}if($_FILES[“mfile”][“name”] ){$randstr = random_str().date(“YmdHis”); //Get the time and assign it to the variable $saveto = $_SERVER[‘DOCUMENT_ROOT’].$path.$randstr.$img_type; //The full path of the image $newimg = $randstr.$img_type; //Picture name$flag=1;}//END IFif ($flag){$result = move_uploaded_file($_FILES[“mfile”][“tmp_name”], $saveto); }//Specially note that the first parameter passed to move_uploaded_file here is the temporary file uploaded to the server $src_file = $saveto;}}list($width_orig, $height_orig) = getimagesize($src_file);if ($width_orig > 680){$tmp_type = exif_imagetype($src_file);switch($tmp_type) {case IMAGETYPE_JPEG :$tmp_src_img = imagecreatefromjpeg($src_file);break;case IMAGETYPE_PNG :$tmp_src_img = imagecreatefrompng($src_file);break;case IMAGETYPE_GIF :$ tmp_src_img = imagecreatefromgif($src_file);break;default:echo “Load image error!”;exit();}$ntmp_width = 680;$ntmp_height = $height_orig*$ntmp_width/$width_orig;$ntmp_imgr = ImageCreateTrueColor($ntmp_width, $ntmp_height);imagecopyresampled($ntmp_imgr, $tmp_src_img, 0, 0, 0, 0, $ntmp_width, $ntmp_height, $width_orig, $height_orig);switch($tmp_type) {case IMAGETYPE_JPEG :header(‘Content-type: image /jpeg’);imagejpeg($ntmp_imgr, $_SERVER[‘DOCUMENT_ROOT’].$path.$randstr.$img_type, 100);break;case IMAGETYPE_PNG :header(‘Content-type: image/png’);imagepng( $ntmp_imgr, $_SERVER[‘DOCUMENT_ROOT’].$path.$randstr.$img_type, 100);break;case IMAGETYPE_GIF :header(‘Content-type: image/gif’);imagegif($ntmp_imgr, $_SERVER[‘ DOCUMENT_ROOT’].$path.$randstr.$img_type, 100);break;default:break;}$src_file = $_SERVER[‘DOCUMENT_ROOT’].$path.$randstr.$img_type;}$type = exif_imagetype($ src_file);switch($type) {case IMAGETYPE_JPEG :$src_img = imagecreatefromjpeg($src_file);break;case IMAGETYPE_PNG :$src_img = imagecreatefrompng($src_file);break;case IMAGETYPE_GIF :$src_img = imagecreatefromgif($src_file);break ;default:echo “Load image error!”;exit();}$dst_r = ImageCreateTrueColor($targ_w, $targ_h);imagecopyresampled($dst_r, $src_img, 0, 0, $x1, $y1, $targ_w, $ targ_h, $targ_w, $targ_h);switch($type) {case IMAGETYPE_JPEG :header(‘Content-type: image/jpeg’);imagejpeg($dst_r, null, 100);break;case IMAGETYPE_PNG :header(‘Content -type: image/png’);imagepng($dst_r, null, 100);break;case IMAGETYPE_GIF :header(‘Content-type: image/gif’);imagegif($dst_r, null, 100);break;default :break;}echo ““;exit;

If I change the red part to the width of the uploaded image and height ($width_orig,$height_orig), it will be abnormal…
According to my current method, is there any way to optimize it?
I see that many people will use imagedestory() after using cropping. This is to delete the image stream, not the original image, right?

The width and height of the image should be obtained through imagesx($src_img), imagesy($src_img), the passed in may be incorrect
imagedestory() is to release resources, such as imagedestory($src_img)
You don’t have to write it, PHP will release it for you

The width and height of the image should be Obtained through imagesx($src_img), imagesy($src_img), the passed in may be incorrect
imagedestory() releases resources, such as imagedestory($src_img)
You don’t need to write it, php will do it for you Released

Thank you. Now I want to save the cropped picture to the database, but the image appears… It cannot be displayed due to an error.

This is the code:

$dst_r = ImageCreateTrueColor($targ_w, $targ_h);
imagecopyresampled($dst_r, $src_img, 0, 0, $x1 , $y1, $targ_w, $targ_h, $targ_w, $targ_h);

$mphoto = $path.$uid.$randstr.$img_type;
$mphoto_save = $_SERVER[ ‘DOCUMENT_ROOT’].$mphoto;
//echo $mphoto_save;
//exit();
switch($type) {
case IMAGETYPE_JPEG :
header (‘Content-type: image/jpeg’);
imagejpeg($dst_r, $mphoto_save, 100);
break;
case IMAGETYPE_PNG :
header(‘Content-type : image/png’);
imagepng($dst_r, $mphoto_save, 100);
break;
case IMAGETYPE_GIF :
header(‘Content-type: image/gif’ );
imagegif($dst_r, $mphoto_save, 100);
break;
default:
break;
}

// echo ““;
//exit();

imagedestory($dst_r);
imagedestory($src_img );

if (!mysqli_query($conn, “update dh_member set photo='”. $mphoto .”‘ where id=”. $uid .”” )){die(mysqli_error($conn));}
echo “”;
exit();

What’s going on? I didn’t even output the picture…

Assume $dst_r is an image resource
imagegif($dst_r); is to output image data stream
imagegif($dst_r, ‘image file name’); is to save to file

Database Just save the image file name
If the database wants to save image data, use the appropriate blob type field
Read the data from the image file or intercept the output of php
Use the bin2hex function to convert it into a string , add ‘0x’ in front

Assume $dst_r is an image resource
imagegif($dst_r); is the output image data stream
imagegif($dst_r, ‘image file name’); is to save to the file

The database can save the image file name
If the database wants to save image data, apply the appropriate blob type Field
Read the data from the image file or intercept the output of php
Use the bin2hex function to convert it into a string, add ‘0x’ in front

Thank you, how? How to prevent it from outputting the image stream (the purpose of outputting the image stream is to save it as a new file name), save it directly to the folder, and then update the database?
nswer”> Assume $dst_r is an image resource
imagegif($dst_r); is to output the image data stream
imagegif($dst_r, ‘image file name’); is to save to the file

The database can save the image file name
If the database wants to save image data, apply the appropriate blob type field
The data is read from the image file or the output of php is intercepted
Use the bin2hex function to convert into a string, add ‘0x’ in front

Assume $dst_r is an image resource
imagegif($dst_r); is to output image data Stream
imagegif($dst_r, ‘image file name’); is to save to the file

The database can save the image file name
If the database wants to save image data, the application is appropriate The blob type field
reads the data from the image file or intercepts the output of php
Use the bin2hex function to convert it into a string and add ‘0x’ in front

Thank you Ah, how to prevent it from outputting the image stream (the purpose of outputting the image stream is to save it as a new file name), save it directly to the folder, and then update the database?

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/php-cutting-problem-please-give-me-some-advice-i-have-been-working-on-it-for-a-day/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索