我有作物的用户配置文件图像的小型定制的Drupal模块:该作品像一个梦时PHP图片裁切误差
$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)''会发生什么? – 2010-01-18 11:39:04
我已经给出了一个去,有趣的是,我无法用提供的图像重现错误(请参阅编辑的问题)。 – 2010-01-18 11:45:59
imagecache模块呢?没有必要重新发明轮子.. – tpow 2010-01-18 17:45:19