【问题标题】:Show cropped image显示裁剪的图像
【发布时间】:2012-09-30 22:02:40
【问题描述】:

我正在使用一个脚本来帮助用户上传他们的个人资料图片,然后裁剪它们以选择方便的位置 我对未显示的 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)){
$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);
     }
      }
     ?>

【问题讨论】:

  • 请查看filter_var 并采用适当的编码风格。

标签: php crop php-gd


【解决方案1】:

你必须检查输入文件的格式,然后调用支持的函数。

如果输入文件是 jpeg,请调用 imagecreatefromjpegimagejpeg
如果输入文件是 png,请调用 imagecreatefrompngimagepng
如果输入文件是 gif,请调用 imagecreatefromjpegimagegif

【讨论】:

  • 或者使用图片数据中的imagecreatefromstring(),确定类型后使用imageTYPE()函数之一。
  • 不起作用我添加了 $type = $_GET['type'];和 elseif ($type = 'png')
  • $_GET['type'] 的值是多少?你100%确定它是正确的吗?此外,您应该使用 switch(){} 而不是巨大的 elseif 块。
  • $_GET['type'] 将获得图像的扩展名。这是完整的网址:data/result_crop_img.php?tr=crop.php?url=img_upload_profile/6348877.jpg&top=0&left =-175&type=png
  • 去掉Content-Type标头,看看是否输出错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2011-12-18
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
相关资源
最近更新 更多