Inhaltsverzeichnis
回复讨论(解决方案)
Heim Backend-Entwicklung PHP-Tutorial PHP 裁切问题,求大神指点指点,弄了一天了.

PHP 裁切问题,求大神指点指点,弄了一天了.

Jun 23, 2016 pm 01:33 PM

我前端用JCROP获取到x、y坐标、宽度、高度,传递到 uphoto.php处理,现在一切正常,但是如果前端用户选择的图片宽度过大(我用css样式控制用户选择的图片最大宽度为680px),裁切出来的就不正常了...我查阅了一些资料,说是需要按比例换算,但是是按什么比例换算啊,用户选择的图片的宽度(大于680px时)和680换算么?

这是 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"])){ //提取文件域内容名称,并判断$path = "/upload/user/photo/"; //上传路径//检查是否有该文件夹,如果没有就创建,并给予最高权限//if(!file_exists($path)){mkdir("$path", 0700);}//允许上传的文件格式$allow_type = array("image/gif","image/pjpeg","image/jpeg","image/png");//检查上传文件是否在允许上传的类型if(!in_array($_FILES["mfile"]["type"], $allow_type)){echo "error:格式有误,仅支持gif/jpg/png"; exit();}//检测图片大小if($_FILES["mfile"]["size"] > 5*1024*1024){echo "error:文件不得超过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"); //获取时间并赋值给变量$saveto = $_SERVER['DOCUMENT_ROOT'].$path.$randstr.$img_type; //图片的完整路径$newimg = $randstr.$img_type; //图片名称$flag=1;}//END IFif ($flag){$result = move_uploaded_file($_FILES["mfile"]["tmp_name"], $saveto);}//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件$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:当前图片格式非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."<br />";//echo $new_height_orig."<br />";//echo "x:".$x1."<br />";//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 "<img  src='". $dst_r ."' / alt="PHP 裁切问题,求大神指点指点,弄了一天了." >";exit;
Nach dem Login kopieren


求大神指点指点


回复讨论(解决方案)

用css样式控制用户选择的图片最大宽度为680px
就表示用户看到的是经过动态缩放的副本
那么你在传递裁剪区域坐标的时候,还需传递图片的原始尺寸
换算一下就可以了
X= x/680*图片宽

用css样式控制用户选择的图片最大宽度为680px
就表示用户看到的是经过动态缩放的副本
那么你在传递裁剪区域坐标的时候,还需传递图片的原始尺寸
换算一下就可以了
X= x/680*图片宽



谢谢啊,按你那样换算了 还是不正常..

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);

你 y 方向没有计算

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;
}
imagecopyresampled($dst_r, $src_img, 0, 0, $nx, $ny, $targ_w, $targ_h, $targ_w, $targ_h);

这样计算么,也还是不行..

$targ_w, $targ_h 这两个是裁切的宽度和高度 会不会跟这两个有关呢?

可以参考一下我之前写的裁剪。因为你是要裁剪,所以宽和高有一个是和裁剪后的尺寸的宽和高一样,另一边会>=裁剪后的。
http://blog.csdn.net/fdipzone/article/details/9316385

    /** 获取目标图生成的size     * @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);      } 
Nach dem Login kopieren
Nach dem Login kopieren

可以参考一下我之前写的裁剪。因为你是要裁剪,所以宽和高有一个是和裁剪后的尺寸的宽和高一样,另一边会>=裁剪后的。
http://blog.csdn.net/fdipzone/article/details/9316385

    /** 获取目标图生成的size     * @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);      } 
Nach dem Login kopieren
Nach dem Login kopieren



谢谢啊 , 按你那样调整了,还是不正常的额:

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, $x1, $y1, $targ_w, $targ_h, $pic_w, $pic_h);

我现在是想把宽度680的图片裁切成宽度680px,然后在裁切...

宽度大于680的图片裁切成宽度680px
由 图片宽 / 图片高 = 680 / h
得 h = 680 / 图片宽 * 图片高

$targ_h = 680 / $pic_w * $pic_h;
$targ_w = 680;

宽度大于680的图片裁切成宽度680px
由 图片宽 / 图片高 = 680 / h
得 h = 680 / 图片宽 * 图片高

$targ_h = 680 / $pic_w * $pic_h;
$targ_w = 680;



谢谢啊,我现在通过将用户选择的图片若超过680px就裁切成宽度680px(高度通过你那样的算法算的),然后再裁切成头像,测试是正常的,但还有几个细节我有点不明白,下面是源码:


$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"])){ //提取文件域内容名称,并判断$path = "/upload/user/photo/"; //上传路径//检查是否有该文件夹,如果没有就创建,并给予最高权限//if(!file_exists($path)){mkdir("$path", 0700);}//允许上传的文件格式$allow_type = array("image/gif","image/pjpeg","image/jpeg","image/png");//检查上传文件是否在允许上传的类型if(!in_array($_FILES["mfile"]["type"], $allow_type)){echo "error:格式有误,仅支持gif/jpg/png"; exit();}//检测图片大小if($_FILES["mfile"]["size"] > 5*1024*1024){echo "error:文件不得超过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"); //获取时间并赋值给变量$saveto = $_SERVER['DOCUMENT_ROOT'].$path.$randstr.$img_type; //图片的完整路径$newimg = $randstr.$img_type; //图片名称$flag=1;}//END IFif ($flag){$result = move_uploaded_file($_FILES["mfile"]["tmp_name"], $saveto);}//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件$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 "<img  src='". $dst_r ."' / alt="PHP 裁切问题,求大神指点指点,弄了一天了." >";exit;
Nach dem Login kopieren


其中那个红色部分,我如果换成上传的图片的宽度和高度($width_orig,$height_orig),就会不正常...
按我现在这样的方式,有没办法优化下?
我看好多使用裁切之后 都会 imagedestory() 这个是删除图片流,而不是原图吧?

图片的宽、高 应通过 imagesx($src_img)、imagesy($src_img) 取得,传入的可能不正确
 imagedestory() 是释放资源,比如  imagedestory($src_img)
不写也可以,php 会替你释放的
 

图片的宽、高 应通过 imagesx($src_img)、imagesy($src_img) 取得,传入的可能不正确
 imagedestory() 是释放资源,比如  imagedestory($src_img)
不写也可以,php 会替你释放的
 



谢谢啊 我现在想把裁切好的图片保存到数据库中,却出现 图像 ... 因存在错误而无法显示。

这是代码:

$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 "PHP 裁切问题,求大神指点指点,弄了一天了.";
//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 "
<script>document.back_form.submit();</script>";
exit();

是怎么回事啊,我都没有输出图片啊..

假定 $dst_r 是 image 资源
imagegif($dst_r); 就是输出图片数据流
imagegif($dst_r, '图片文件名'); 就是保存到文件

数据库保存图片文件名即可
如果数据库要保存图片数据,则应用合适的 blob 类型字段 
数据从 图片文件 读取或拦截php的输出
用 bin2hex 函数转换成字符串,在前面加 ‘0x'

假定 $dst_r 是 image 资源
imagegif($dst_r); 就是输出图片数据流
imagegif($dst_r, '图片文件名'); 就是保存到文件

数据库保存图片文件名即可
如果数据库要保存图片数据,则应用合适的 blob 类型字段 
数据从 图片文件 读取或拦截php的输出
用 bin2hex 函数转换成字符串,在前面加 ‘0x'



谢谢啊,怎么做到让它不输出图片流呢(输出图片流为的就是保存成新文件名),直接保存到文件夹,然后更新数据库呢?
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) 11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) Mar 03, 2025 am 10:49 AM

Lange URLs, die oft mit Schlüsselwörtern und Tracking -Parametern überfüllt sind, können Besucher abschrecken. Ein URL -Verkürzungsskript bietet eine Lösung, die präzise Links erstellt, die ideal für soziale Medien und andere Plattformen sind. Diese Skripte sind für einzelne Websites a wertvoll

Einführung in die Instagram -API Einführung in die Instagram -API Mar 02, 2025 am 09:32 AM

Nach seiner hochkarätigen Akquisition durch Facebook im Jahr 2012 nahm Instagram zwei APIs für den Einsatz von Drittanbietern ein. Dies sind die Instagram -Graph -API und die Instagram Basic Display -API. Ein Entwickler, der eine App erstellt, die Informationen von a benötigt

Arbeiten mit Flash -Sitzungsdaten in Laravel Arbeiten mit Flash -Sitzungsdaten in Laravel Mar 12, 2025 pm 05:08 PM

Laravel vereinfacht die Behandlung von temporären Sitzungsdaten mithilfe seiner intuitiven Flash -Methoden. Dies ist perfekt zum Anzeigen von kurzen Nachrichten, Warnungen oder Benachrichtigungen in Ihrer Anwendung. Die Daten bestehen nur für die nachfolgende Anfrage standardmäßig: $ Anfrage-

Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Mar 12, 2025 pm 05:09 PM

Laravel bietet eine kurze HTTP -Antwortsimulationssyntax und vereinfache HTTP -Interaktionstests. Dieser Ansatz reduziert die Code -Redundanz erheblich, während Ihre Testsimulation intuitiver wird. Die grundlegende Implementierung bietet eine Vielzahl von Verknüpfungen zum Antworttyp: Verwenden Sie Illuminate \ Support \ facades \ http; Http :: fake ([ 'Google.com' => 'Hallo Welt',, 'github.com' => ['foo' => 'bar'], 'Forge.laravel.com' =>

Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Mar 04, 2025 am 09:33 AM

Dies ist der zweite und letzte Teil der Serie zum Aufbau einer Reaktionsanwendung mit einem Laravel-Back-End. Im ersten Teil der Serie haben wir eine erholsame API erstellt, die Laravel für eine grundlegende Produktlistenanwendung unter Verwendung von Laravel erstellt hat. In diesem Tutorial werden wir Dev sein

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Mar 14, 2025 am 11:42 AM

Die PHP Client -URL -Erweiterung (CURL) ist ein leistungsstarkes Tool für Entwickler, das eine nahtlose Interaktion mit Remote -Servern und REST -APIs ermöglicht. Durch die Nutzung von Libcurl, einer angesehenen Bibliothek mit Multi-Protokoll-Dateien, erleichtert PHP Curl effiziente Execu

12 Beste PHP -Chat -Skripte auf Codecanyon 12 Beste PHP -Chat -Skripte auf Codecanyon Mar 13, 2025 pm 12:08 PM

Möchten Sie den dringlichsten Problemen Ihrer Kunden in Echtzeit und Sofortlösungen anbieten? Mit Live-Chat können Sie Echtzeitgespräche mit Kunden führen und ihre Probleme sofort lösen. Sie ermöglichen es Ihnen, Ihrem Brauch einen schnelleren Service zu bieten

Ankündigung von 2025 PHP Situation Survey Ankündigung von 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Die 2025 PHP Landscape Survey untersucht die aktuellen PHP -Entwicklungstrends. Es untersucht Framework -Nutzung, Bereitstellungsmethoden und Herausforderungen, die darauf abzielen, Entwicklern und Unternehmen Einblicke zu geben. Die Umfrage erwartet das Wachstum der modernen PHP -Versio

See all articles