PHP cut the problem, ask the big God pointing, got a day.

Source: Internet
Author: User
Tags imagejpeg
I used Jcrop to get x, y coordinates, width, height, passed to uphoto.php processing, now everything is normal, but if the front-end user chooses the picture width is too large (I use CSS style control user selected picture maximum width is 680px), cut out of is not normal ... I looked up some information, said to be proportional conversion, but by what proportion of Ah, the user selected the width of the picture (greater than 680px) and 680 conversion?

This is uphoto.php:

$x 1 = $_post["x1"]; $x 2 = $_post["X2"]; $y 1 = $_post["Y1"]; $y 2 = $_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 if there is a folder, if not, create it, and give the highest privilege//if (!file_exists ($path)) {mkdir ("$path", 0700);} Allow upload of file format $allow_type = Array ("Image/gif", "Image/pjpeg", "Image/jpeg", "image/png");//Check whether the upload file is in the type that is allowed to upload if (!in_ Array ($_files["Mfile" ["type"], $allow _type)) {echo "Error: Format is incorrect, support only Gif/jpg/png"; exit ();} Detect picture 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 time and assign value to variable $saveto = $_server[' Document_root ']. $path. $randstr. $img _type; The full path of the picture $newimg = $randstr. $img _type; Picture name $flag=1;} END IfIf ($flag) {$result = Move_uploaded_file ($_files["Mfile" ["Tmp_name"], $saveto);} Note in particular that the first parameter passed to Move_uploaded_file is a 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: Current picture 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: ". $x 1."
";//echo" y: ". $y 1;//exit (); imagecopyresampled ($dst _r, $src _img, 0, 0, $x 1, $y 1, $targ _w, $targ _h, $targ _w, $targ _h);// Imagecopyresampled ($dst _r, $src _img, 0, 0, $x 1, $y 1, $targ _w, $targ _h, $targ _w, $targ _h); switch ($type) {case IMAGETYPE_JP Eg:header (' content-type:image/jpeg '); imagejpeg ($dst _r, NULL, n); break;case imagetype_png:header (' Content-type: Image/png '); Imagepng ($dst _r, NULL, n); break;case imagetype_gif:header (' content-type:image/gif '); Imagegif ($dst _r , null, n); break;default:break;} echo ""; exit;


Ask the great God for guidance


Reply to discussion (solution)

Use CSS styles to control user-selected images with a maximum width of 680px
means that the user sees a dynamically scaled copy
So when you pass the crop area coordinates, you need to pass the original size of the picture.
You can convert it.
x= x/680* Picture Width

Use CSS styles to control user-selected images with a maximum width of 680px
means that the user sees a dynamically scaled copy
So when you pass the crop area coordinates, you need to pass the original size of the picture.
You can convert it.
x= x/680* Picture Width



Thank you, according to your conversion or not normal.

List ($width _orig, $height _orig) = getimagesize ($src _file);
if ($width _orig > 680) {
$NX = $x 1/680 * $width _orig;
}
else {
$nx = $x 1;
}

Imagecopyresampled ($dst _r, $src _img, 0, 0, $nx, $y 1, $targ _w, $targ _h, $targ _w, $targ _h);

You have no calculations in y direction.

List ($width _orig, $height _orig) = getimagesize ($src _file);
if ($width _orig > 680) {
$NX = $x 1/680 * $width _orig;
$new _height = $height _orig*680/$width _orig;
$ny = $y 1/$new _height * $height _orig;
}
else {
$nx = $x 1;
$ny = $y 1;
}
Imagecopyresampled ($dst _r, $src _img, 0, 0, $nx, $ny, $targ _w, $targ _h, $targ _w, $targ _h);

This calculation, it is still not possible.

$targ _w, $targ _h These two are the width and height of the crop will not be related to these two?

You can refer to the crop I wrote earlier. Because you are going to cut, so the width and height have one and the size of the width and height of the cut, the other side will be >= cropped.
http://blog.csdn.net/fdipzone/article/details/9316385

/** gets the size * generated by the target graph * @return Array $width, $height */Private Function get_          Size () {list ($owidth, $oheight) = getimagesize ($this->_source);          $width = (int) ($this->_width);                    $height = (int) ($this->_height);                  Switch ($this->_type) {case ' fit ': $pic _w = $width;                  $pic _h = (int) ($pic _w* $oheight/$owidth);                      if ($pic _h> $height) {$pic _h = $height;                  $pic _w = (int) ($pic _h* $owidth/$oheight);              } break;                  Case ' crop ': $pic _w = $width;                  $pic _h = (int) ($pic _w* $oheight/$owidth);                      if ($pic _h< $height) {$pic _h = $height;                  $pic _w = (int) ($pic _h* $owidth/$oheight);          } break;      } return Array ($pic _w, $pic _h); } 

You can refer to the crop I wrote earlier. Because you are going to cut, so the width and height have one and the size of the width and height of the cut, the other side will be >= cropped.
http://blog.csdn.net/fdipzone/article/details/9316385

/** gets the size * generated by the target graph * @return Array $width, $height */Private Function get_          Size () {list ($owidth, $oheight) = getimagesize ($this->_source);          $width = (int) ($this->_width);                    $height = (int) ($this->_height);                  Switch ($this->_type) {case ' fit ': $pic _w = $width;                  $pic _h = (int) ($pic _w* $oheight/$owidth);                      if ($pic _h> $height) {$pic _h = $height;                  $pic _w = (int) ($pic _h* $owidth/$oheight);              } break;                  Case ' crop ': $pic _w = $width;                  $pic _h = (int) ($pic _w* $oheight/$owidth);                      if ($pic _h< $height) {$pic _h = $height;                  $pic _w = (int) ($pic _h* $owidth/$oheight);          } break;      } return Array ($pic _w, $pic _h); } 



Thank you, as you have adjusted, or abnormal amount:

List ($width _orig, $height _orig) = getimagesize ($src _file);
$pic _w = $targ _w;
$pic _h = (int) ($pic _w* $height _orig/$width _orig);
if ($pic _h < $targ _h) {
$pic _h = $targ _h;
$pic _w = (int) ($pic _h* $width _orig/$height _orig);
}
Imagecopyresampled ($dst _r, $src _img, 0, 0, $x 1, $y 1, $targ _w, $targ _h, $pic _w, $pic _h);

Now I want to cut the width of 680 of the picture into a width of 680px, and then cut ...

Picture width greater than 680 cut to width 680px
by picture width/Picture height = 680/h
h = 680/Picture width * Picture high
That
$targ _h = 680/$pic _w * $pic _h;
$targ _w = 680;

Picture width greater than 680 cut to width 680px
by picture width/Picture height = 680/h
h = 680/Picture width * Picture high
That
$targ _h = 680/$pic _w * $pic _h;
$targ _w = 680;



Thank you Ah, I now by the user selected picture if more than 680px cut cut into width 680px (height through your algorithm), and then cut into the head, test is normal, but there are a few details I do not understand, the following is the source:


$x 1 = $_post["x1"]; $x 2 = $_post["X2"]; $y 1 = $_post["Y1"]; $y 2 = $_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 if there is a folder, if not, create it, and give the highest privilege//if (!file_exists ($path)) {mkdir ("$path", 0700);} Allow upload of file format $allow_type = Array ("Image/gif", "Image/pjpeg", "Image/jpeg", "image/png");//Check whether the upload file is in the type that is allowed to upload if (!in_ Array ($_files["Mfile" ["type"], $allow _type)) {echo "Error: Format is incorrect, support only Gif/jpg/png"; exit ();} Detect picture 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 time and assign value to variable $saveto = $_server[' Document_root ']. $path. $randstr. $img _type; The full path of the picture $newimg = $randstr. $img _type; Picture name $flag=1;} END IfIf ($flag) {$result = Move_uploaded_file ($_files["Mfile" ["Tmp_name"], $saveto);} Note in particular that the first parameter passed to Move_uploaded_file is a 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 ');Magejpeg ($ntmp _imgr, $_server[' Document_root '). $path. $randstr. $img _type, +); Break;case Imagetype_png:header (' Content-type:image/png '); Imagepng ($ntmp _imgr, $_server[' Document_root '). $path. $randstr. $img _type; Case Imagetype_gif:header (' content-type:image/gif '); Imagegif ($ntmp _imgr, $_server[' Document_root '). $path. $ Randstr. $img _type); 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); 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, $x 1, $y 1, $targ _w, $targ _h, $t Arg_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, n); break;case imagetype_gif:header (' content-type:image/gif ') ; Imagegif ($dst _r, NULL, n); break;default:break;} echo ""; exit;


One of the red parts, if I change the width and height of the uploaded image ($width _orig, $height _orig), it will not be normal ...
According to the way I am now, is there any way to optimize it?
I see a lot of the use of cutting after the imagedestory () this is to delete the picture stream, not the original?

The width and height of the picture should be obtained through IMAGESX ($src _img), Imagesy ($src _img), which may be incorrect
Imagedestory () is the release of resources, such as Imagedestory ($src _img)
If you don't write it, PHP will release it for you.

The width and height of the picture should be obtained through IMAGESX ($src _img), Imagesy ($src _img), which may be incorrect
Imagedestory () is the release of resources, such as Imagedestory ($src _img)
If you don't write it, PHP will release it for you.



Oh, thank you. I want to save the cut picture to the database, but the image appears ... cannot be displayed because of an error.

This is the code:

$DST _r = Imagecreatetruecolor ($targ _w, $targ _h);
Imagecopyresampled ($dst _r, $src _img, 0, 0, $x 1, $y 1, $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 the matter ah, I have no output pictures ah.

Assume that $dst _r is an image resource
Imagegif ($dst _r); Is the output picture data stream
Imagegif ($dst _r, ' picture filename '); is to save to a file

The database can save the picture file name
If the database is saving picture data, apply the appropriate BLOB type field
Data reads or blocks PHP output from a picture file
Use the Bin2Hex function to convert to a string, plus ' 0x ' in front

Assume that $dst _r is an image resource
Imagegif ($dst _r); Is the output picture data stream
Imagegif ($dst _r, ' picture filename '); is to save to a file

The database can save the picture file name
If the database is saving picture data, apply the appropriate BLOB type field
Data reads or blocks PHP output from a picture file
Use the Bin2Hex function to convert to a string, plus ' 0x ' in front



Thank you, ah, how do you let it not output picture stream (the output picture stream is saved as a new file name), directly saved to the folder, and then update the database?
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.