Search code examples
phplaravelintervention

Unable to decode input | Laravel | Image Intervention


in following code i am when passing image from my public_path its working but when i am padding another image as URL i am getting error like "Unable to decode input". i am using Laravel 11, Intervention package version 3 i.e latest & PHP version 8.2

// create an test image from a file
$manager = ImageManager::gd();
$image = $manager->read(public_path('images/templates/id-card.png'));

$anotherimage = $manager->read('https://static-00.iconduck.com/assets.00/user-icon-1024x1024-dtzturco.png');
// resize to 300 x 200 pixel
$anotherimage->resize(300, 200);

// resize only image height to 200 pixel
$anotherimage->resize(height: 200);


// paste another image
$image = $image->place(
    $anotherimage,
    10, 
    10,
    25
);

// Encode the image to JPG format (using encode method properly)
$encodedImage = $image->encode();  // This should work if 'jpg' is passed as a string

// Check if encoding works properly
if (!$encodedImage) {
    return response('Error encoding image', 500);
}

// Stream the image response with correct headers
return Response::make($encodedImage, 200, [
    'Content-Type' => 'image/jpeg',
]);

Solution

  • Image intervention V3 has so many changes and new functions. which we generally used in V2 is not available in V3 as we are doing in V2 so i have resolved the issue by doing following.

    // create an test image from a file
    $manager = ImageManager::gd();
    
    // nonmal image called from storage path
    $image = $manager->read(public_path('images/templates/id-card.png'));
    
    // image called from URL have to do like below
    $anotherimage = file_get_contents('https://plus.unsplash.com/premium_photo-1683121366070-5ceb7e007a97?fm=jpg&q=60&w=3000&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MXx8dXNlcnxlbnwwfHwwfHx8MA%3D%3D');
    
    $anotherimage = $manager->read($anotherimage);
    

    by just initially adding "file_get_contents()" you can read the image in Image intervention V3