【问题标题】:PHP image crop errorPHP图像裁剪错误
【发布时间】:2010-01-18 10:48:13
【问题描述】:

我有一个小的自定义 drupal 模块,可以裁剪用户个人资料图像:

$folder = 'sites/default/files/profile_pictures/'.$uid.'/';

$filename = $_POST['filename'];
$orig_w = 480;
$orig_h = $_POST['height'];

$targ_w = 150;
$targ_h = 92;

$ratio = $targ_w / $targ_h;

if(isset($_POST['form_sent']))
{   

    $src = imagecreatefromjpeg($folder.$filename);

    $tmp = imagecreatetruecolor($targ_w, $targ_h);
    imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
    imagejpeg($tmp, $folder.'t_'.$filename,100);

    $full_path = $folder.'t_'.$filename;

    imagedestroy($tmp);
    imagedestroy($src);

    // database stuff

} else {
    return 'Nothing to crop!';
}

95% 的情况下,这就像一场梦,但偶尔会返回黑色图像。

这是一个问题的例子: http://5oup.net/sites/default/files/profile_pictures/405/t_Photo_8.jpg

和原始文件: http://5oup.net/sites/default/files/profile_pictures/405/Photo_8.jpg

知道可能出了什么问题吗?我的猜测是 imagecreatetruecolor() 线附近的东西?

提前致谢

詹姆斯

编辑

我似乎无法用原始用户图像重现错误,所以我现在真的不确定在这种情况下是什么原因造成的!有一个裁剪前的图像上传功能,我现在意识到这也可能是错误的根源,所以为了充分披露:

if( isset($_POST['form_sent']) )
{
    $imageFile = $_FILES['image']['tmp_name'];
    $filename = basename( $_FILES['image']['name']);
    $filename = preg_replace("/[^A-Za-z0-9 .]/", '', $filename);
    $filename = str_replace(" ", '_', $filename);

    $filename = urlencode($filename);

    $img_info = getimagesize($imageFile);

    if (filesize($imageFile) > 510000) {
        drupal_set_message('Image too large. Please upload a file 500kb or less.', 'error');
        $image_form = '<a href="/avatar">Choose a different image</a>';
        $output = array(
            'image_form' => $image_form
        );
        return serialize($output);
    }

    list($width, $height) = getimagesize($imageFile);

    switch ($img_info['mime']) {
        case 'image/png':
        $src = imagecreatefrompng($imageFile);
        break;

        case 'image/jpeg':
        case 'image/jpg':
        $src = imagecreatefromjpeg($imageFile);
        break;

        case 'image/gif':
        $src = imagecreatefromgif($imageFile);
        break;

        default:
        drupal_set_message('File type not allowed. Please upload a jpg, gif or png.', 'error');
            $image_form = '<a href="/avatar">Jpg, gif or pngs only</a>';
            $output = array(
                'image_form' => $image_form
            );
            return serialize($output);
        break;
    }

    $orig_h = ($height/$width)* $orig_w;

    $tmp = imagecreatetruecolor($orig_w, $orig_h);
    imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
    imagejpeg($tmp, $folder.$filename, 100);

    imagedestroy($tmp);
    imagedestroy($src);     


}

【问题讨论】:

  • 如果在代码前添加ini_set('gd.jpeg_ignore_warning', 1); 会发生什么?
  • 我已经尝试过了,有趣的是我无法使用提供的图像重现错误(请参阅编辑后的问题)。
  • imagecache 模块呢?无需重新发明轮子..
  • 我们最初使用的是图像缓存,但可惜与服务器发生了某种冲突,所有图像都被破坏了,所以我们决定自己推出。
  • 我这里有确切的问题。仅限特定照片。我相信这是与 GD 库相关的问题。

标签: php image drupal


【解决方案1】:

文件不存在。 如果有空格,您的第一个函数会重命名文件,然后将其传递给缩放函数。

但是,在缩放功能中,您将文件名重置为帖子名称(上传的文件)。因此,该函数会去寻找一个不存在的文件(因为它已被重命名),然后只返回您创建的涂黑本色。

摆脱将文件名重置为第一个帖子(原始函数中的第二行)的代码,你应该没问题。此外,我会在制作缩略图之前进行 is_file 检查。

【讨论】:

  • 感谢您的回复!这些函数从不同的页面调用,因此第一个文件名作为隐藏表单值传递给第二个,然后使用 $_POST['filename'] 再次检索。同样正如我所说,它在大多数情况下都可以正常工作,只是偶尔出现错误。
  • 但是,它在那些带有空格的典型文件中不起作用吗?我会尝试一个 is_file 并返回一个错误,看看这是否是问题所在,如果你能够复制,你说你可以。
  • 不幸的是,我无法复制该问题(请参阅我编辑的问题),但我确实有一个显示问题的前后文件示例。错误文件中很有可能有空格,所以我会给出您的 is_file() 建议并报告回来。
  • 好的,这是另一个问题:原件的大小可能小于 500K,但它仍然可能使用比重采样过程更多的内存。坏图像的原始文件大小是多少(应该很容易弄清楚)。
  • 我在问题中给出的文件是 68k,所以我想那里没有问题?
【解决方案2】:
@RegisterRestClient(baseUri = "http://localhost:8080/StudentDemo/rest/student")
public interface RemoteClient {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<Student> get();

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void insertStudent(Student stud);

    @DELETE
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public void deleteStudent(@PathParam("id")int id);
 
    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public void updateStudent(@PathParam("id") int id, Student stud);
}

【讨论】:

  • 您能否详细说明您的答案并解释它如何解决所提出的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 2015-02-18
  • 2011-01-01
相关资源
最近更新 更多