tags:

views:

159

answers:

1

Hello again!

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('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);     


}
A: 

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.

Cryophallion
Thanks for your reply!These functions are called from diffent pages so the filename from the first is passed to the second as a hidden form value, then retreived again using $_POST['filename']. Also as I say it works as is most of the time, just the occasional error.
James Chambers
But are the times that it doesn't work the typically files with spaces in them? I would try an is_file and return an error to see if that is the issue, if you are able to replicate, which you say you can.
Cryophallion
Unfortunatley I'm not able to replicate the issue (see my edited question), but I do have an example of a before and after file which exhibits the problem. There is a good chance that the error file has a space in it so I will give your is_file() suggestion and report back.
James Chambers
Ok, here's another issue: Size of the original may be less than 500K, but it still may be using more memory than that for the resampling process. What are the original filesizes for the bad images (that should be easy enough to figure out).
Cryophallion
The file I gave in my question is 68k so i guess no problem there?
James Chambers
Probably not, I'm just troubleshooting based on issues I've run into in the past.
Cryophallion