Php 显示裁剪图像
我正在使用一个脚本,帮助用户上传他们的个人资料图像,然后裁剪它们以选择方便的位置 我对未显示的PNG和GIF图像有问题 这是裁剪文件代码,请修改它以使其能够显示PNG、JPF和GIF图像Php 显示裁剪图像,php,crop,php-gd,Php,Crop,Php Gd,我正在使用一个脚本,帮助用户上传他们的个人资料图像,然后裁剪它们以选择方便的位置 我对未显示的PNG和GIF图像有问题 这是裁剪文件代码,请修改它以使其能够显示PNG、JPF和GIF图像 <?php sleep(1); $url = $_GET['url']; $type = $_GET['type']; if ($type='jpg') { if(file_exists($url) AND preg_match('/^[a-z0-9\/_\-.]+$/i',$url)){
<?php
sleep(1);
$url = $_GET['url'];
$type = $_GET['type'];
if ($type='jpg') {
if(file_exists($url) AND preg_match('/^[a-z0-9\/_\-.]+$/i',$url)){
$width = (isset($_GET['width']) AND preg_match('/^[0-9]{2,}$/', $_GET['width'])) ? $_GET['width'] : 300;
$height = (isset($_GET['height']) AND preg_match('/^[0-9]{2,}$/', $_GET['height'])) ? $_GET['height'] : 300;
$left = (isset($_GET['left']) AND is_numeric($_GET['left'])) ? $_GET['left'] : 0;
$top = (isset($_GET['top']) AND is_numeric($_GET['top'])) ? $_GET['top'] : 0;
header ("Content-type: image/jpg");
$src = @imagecreatefromjpeg($url);
$im = @imagecreatetruecolor($width, $height);
imagecopy($im,$src,0,0,-$left,-$top,$width,$height);
imagejpeg($im,"",300);
imagedestroy($im);}}
elseif ($type='png') {
if(file_exists($url) AND preg_match('/^[a-z0-9\/_\-.]+$/i',$url)){
$width = (isset($_GET['width']) AND preg_match('/^[0-9]{2,}$/', $_GET['width'])) ? $_GET['width'] : 300;
$height = (isset($_GET['height']) AND preg_match('/^[0-9]{2,}$/', $_GET['height'])) ? $_GET['height'] : 300;
$left = (isset($_GET['left']) AND is_numeric($_GET['left'])) ? $_GET['left'] : 0;
$top = (isset($_GET['top']) AND is_numeric($_GET['top'])) ? $_GET['top'] : 0;
header ("Content-type: image/png");
$src = @imagecreatefrompng($url);
$im = @imagecreatetruecolor($width, $height);
imagecopy($im,$src,0,0,-$left,-$top,$width,$height);
imagepng($im,"",300);
imagedestroy($im);
}
}
?>
您必须检查输入文件的格式,然后调用支持的函数 如果输入文件是jpeg,请调用
imagecreatefromjpeg
和imagejpeg
如果输入文件是png,请调用
imagecreatefrompng
和imagepng
如果输入文件是gif,请调用
imagecreatefromjpeg
和imagegif
或者从图像数据中使用imagecreatefromstring()
,并在确定其类型后使用imageTYPE()
函数之一。不起作用我添加了$type=$\u GET['type'];和elseif($type='png')$\u GET['type']的值是多少?你100%确定它是正确的吗?另外,您应该使用switch(){}
而不是巨大的elseif
块将获得图像的扩展。这是完整的url:data/result\u crop\u img.php?tr=crop.php?url=img\u upload\u profile/6348877.jpg&top=0&left=-175&type=png移动内容类型
标题,查看是否输出错误。查看过滤器变量
并采用适当的编码样式。