malesandro's avatar

Problem with vagrant and images

Hello everybody! I am running into a problem with Homestead. I have a piece of code that works well on an online dev server, but fails in the vagrant Homestead one.

The piece of code is an ajax executed one, where I upload an image, save it in a temp directory and send it back to the user, who then crops it. For this, I have two functions, tempUpload and tempCrop. It is failing in tempCrop, and most the line that triggers it is the following:

$img = getimagesize($imgUrl);

$imgUrl is an input with the url to an image in a temp folder. Checking in vagrant, I saw that these images have attributes -rwxrwxrwx 1 vagrant vagrant. The error appearing in the console is "SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data".

Again, this works perfect in an online version, so I guess it is either a permission problem or some environment setting. The permissions for the temp folder in vagrant are as following: drwxrwxrwx 1 vagrant vagrant

This is the expanded code:

$imgUrl = $input['imgUrl'];
$imgInitW = $input['imgInitW'];
$imgInitH = $input['imgInitH'];
$imgW = $input['imgW'];
$imgH = $input['imgH'];
$imgY1 = $input['imgY1'];
$imgX1 = $input['imgX1'];
$cropW = $input['cropW'];
$cropH = $input['cropH'];

$jpeg_quality = 100;


$directory = 'temp/';
$filename = sha1(time().time());


$output_filename = $directory.$filename."_cropped";

$img  = getimagesize($imgUrl);

switch(strtolower($img['mime']))
{
    case 'image/png':
        $img_r = imagecreatefrompng($imgUrl);
        $source_image = imagecreatefrompng($imgUrl);
        $type = '.png';
        break;
    case 'image/jpeg':
        $img_r = imagecreatefromjpeg($imgUrl);
        $source_image = imagecreatefromjpeg($imgUrl);
        $type = '.jpg';
        break;
    case 'image/gif':
        $img_r = imagecreatefromgif($imgUrl);
        $source_image = imagecreatefromgif($imgUrl);
        $type = '.gif';
        break;
    default: $options = array(
            "status" => "error",
            "message" => "Please upload only images, with JPG, GIF or PNG format."); 

              print json_encode($options);
        break;
        exit();
}

$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, 
            $imgH, $imgInitW, $imgInitH);   


$dest_image = imagecreatetruecolor($cropW, $cropH);
imagecopyresampled($dest_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, 
            $cropH, $cropW, $cropH);    


imagejpeg($dest_image, $output_filename.$type, $jpeg_quality);
//Creating thumbnail to user
$img_temp = Image::make(URL::asset($output_filename.$type));
//Image::make(URL::asset($output_filename.$type))->fit(250, 150)->save('temp/'.$output_filename.'_desthumb'.$type,100);
$img_temp->fit(304, 150)->save($output_filename.'_desthumb'.$type,100);
$thumb304x150 = $output_filename.'_desthumb'.$type;
$response = array(
        "status" => "success",
        "url" => URL::asset($thumb304x150) 
      );
// saving cropped image to session
//Session::put('image1.cropped',$output_filename.$type);
Session::put($input['order'].'_cropped', $output_filename.$type);
File::delete(public_path(Session::get($input['order'].'_original')));
print json_encode($response);

Any ideas?

0 likes
4 replies
henrique's avatar

I think the error you got is the javascript error because the server didn't return valid JSON. Go to the network tab to check for server error message and post it here :)

malesandro's avatar

@henrique yeap, that is the problem. The server is returning a 500 error, so it is breaking while trying to crop. Since this only happens while testing with homestead/vagrant, I believe it is something related to file permissions or similar, but I cannot really pinpoint the cause. I posted here hoping that someone had a similar error while dealing with image manipulation in this setup... Thanks, Mariano

Please or to participate in this conversation.