开发者

PHP image crop error

I have a small custom drupal module which crops user profile images:

$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% of the time this works like a dream, however occasionally it will return a black image.

Here is an example of the problem: http://5oup.net/sites/default/files/profile_pictures/405/t_Photo_8.jpg

and the original file: http://5oup.net/sites/default/files/profile_pictures/405/Photo_8.jpg

Any idea what might be going wrong? My guess is something around the imagecreatetruecolor() line?

Thanks in advance

James

EDIT

I can't seem to reproduce the error with the orginal user image so I'm now really unsure what has caused it in this case! There is a pre-crop image upload function which I now realise could also be the source of the error, so in the interests of full disclosure:

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('I开发者_运维知识库mage 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);     


}


The file doesn't exist. Your first function renames the files if there are spaces, then passes it to the scaling function.

However, in the scaling function, you reset the filename to the post name (the uploaded file). So, the function goes to look for a file that doesn't exist (because it has been renamed), and then just returned the blacked out true color you created.

Get rid of code resetting the filename to the post one (the second line in your original function), and you should be fine. Additionally, I would do a is_file check before making the thumbnail.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜