PHP cutting related issues, ask the big God pointing out, got a day.

Source: Internet
Author: User
PHP cut the problem, ask the big God pointing fingers, get a day ...
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 field content name and determine
$path = "/upload/user/photo/"; Upload path
Check if there is a folder, if not created, and give the highest permissions
if (!file_exists ($path)) {mkdir ("$path", 0700);}
Allow upload of file formats
$allow _type = Array ("Image/gif", "Image/pjpeg", "Image/jpeg", "image/png");
Check if the upload file is in a type that allows uploading
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 a value to a variable
$saveto = $_server[' Document_root '). $path. $randstr. $img _type; The full path of the picture
$newimg = $randstr. $img _type; Picture name
$flag = 1;
}
END IF
if ($flag) {$result = Move_uploaded_file ($_files["Mfile" ["Tmp_name"], $saveto);}
Note that the first parameter passed to Move_uploaded_file here 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: The current picture format is not jpg/png/gif"; exit ();}
Load image
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);
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_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




Ask the great God for guidance
------to solve the idea----------------------
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


------to solve the idea----------------------
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);
}
  • 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.