PHP图像裁剪错误

时间:2010-01-18 10:48:14

标签: php image drupal

我有一个小型自定义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);     


}

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

相关问题