Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GD driver is breaking pixels when resizing image #1405

Open
ubedthaheem opened this issue Nov 19, 2024 · 3 comments
Open

GD driver is breaking pixels when resizing image #1405

ubedthaheem opened this issue Nov 19, 2024 · 3 comments

Comments

@ubedthaheem
Copy link

Describe the bug
I tried to implement this library into my Laravel project, apart from its awesome features & easiness in use, this library is breaking pixels in while resizing image with cover() function.

Code Example
Code example to reproduce the behavior.

Expected behavior
foreach ($cropSize as $key => $value) { $resizedName = pathinfo($imageName, PATHINFO_FILENAME) . "_{$key}." . $file->getClientOriginalExtension(); //$image->setResolution($x_resolution, $y_resolution); $image->scale($value['width'], $value['height'])->save(public_path("assets/images/$folderPath/$resizedName")); $resizedImages[] = "assets/images/$folderPath/$resizedName"; }

Images
If applicable, add problematic images or screenshots to help explain your problem.
Screenshot 2024-11-19 at 8 58 23 PM
Screenshot 2024-11-19 at 8 54 24 PM

Environment (please complete the following information):

  • PHP Version: 8.3
  • OS: MacOS
  • Intervention Image Version: latest
  • GD or Imagick: GD
@olivervogel olivervogel transferred this issue from Intervention/image-laravel Nov 19, 2024
@olivervogel
Copy link
Member

olivervogel commented Nov 19, 2024

Please edit your code example with the exact values passed by replacing the variables names with their corresponding values. With just the variables names you provided, I can't see what is going on and what you are trying to do.

@ubedthaheem
Copy link
Author

I created a trait HasMedia that have uploadImage() function with params.
When admin tries to upload an image, it checks either making thumbnail or not, if making thumbnail is true then create few thumbnails from uploaded image and save it to the same directory with thumbnail names.

here is my code:

trait HasMedia
{

    /**
     * Handle Uploading Images
     * $file == $request->file('name')
     * $folderPaht = $request->input('folder_id') {get folder name with ID and store image inside that folder, if it is 0 then upload image on default folder}
     * max size will be 10 MB
     * min height and width = 10x10
     * crop sizes = [150x150, 300x300, 1024x1024]
     * cropped images should be center crop 
     * -- image name structure --
     * defaultPath = storage/images
     * folderPath = storage/images/$folderName
     * name = date{dmY}_random
     * name_resized = date{dmY}_random_100x50 i.e cropped size
     * 
     * @return array ['size', 'full_path', 'file_name , 'mime_type' , 'resized_images ]
    */

    protected function uploadImage($file, $folderPath, $make_thumbnails = false) {
        $manager = ImageManager::gd();
        // Validate file size and dimensions
        $maxSize = config('admin_media.max_size'); // 10 MB in bytes
        $minWidth = 10;
        $minHeight = 10;
    
        $fileSize = $file->getSize();
        $mimeType = $file->getMimeType();
        if ($file->getSize() > $maxSize) {
            return ['error' => true, 'message' => 'File size exceeds the maximum allowed size (10 MB or '.$maxSize.' bytes). Your image size is '.$file->getSize()];
        }

        $image = $manager->read($file);
        $width = $image->width();
        $height = $image->height();
 
 


        if ($width < $minWidth || $height < $minHeight) {
            return ['error' => true, 'message' => 'Image dimensions must be at least 10x10 pixels.'];
        }

        // Generate random name for the image
        $imageName = date('Ymd') . '_' . uniqid() . '.' . $file->getClientOriginalExtension();

        // Upload original image
        $fullPath = $folderPath ? "assets/$folderPath/$imageName" : "assets/images/$imageName";
        $destination = public_path($folderPath ? "assets/$folderPath" : "assets/images");
        $file->move($destination, $imageName);


        /**
         * Make Thumbnail check if it is enabled
         */
        $resizedImages = [];
        if(config('admin_media.make_thumbnails') == true || $make_thumbnails == true) {
            $cropSize = config('admin_media.sizes');
            $thmb_destination = $folderPath ? "assets/$folderPath" : "assets/images";
            foreach ($cropSize as $key => $value) {
                $resizedName = pathinfo($imageName, PATHINFO_FILENAME) . "_{$key}." . $file->getClientOriginalExtension();

                $image->resize($value['width'], $value['height'], function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                })->sharpen(0)->save(public_path("$thmb_destination/$resizedName"), 100);
                $resizedImages[] = "$thmb_destination/$resizedName";
            }
        }
 

        return [
            'size' => $fileSize,
            'file_name' => $imageName,
            'full_path' => $fullPath,
            'mime_type' => $mimeType,
            'resized_images' => $resizedImages
        ];
    }
}

@olivervogel
Copy link
Member

Here again, I cannot see which values arrive in the resize method.

Please provide a code example with the exact values passed by replacing the variables names passed to the resize() method with their corresponding values. With just the variables names you provided, I can't see what is going on and what you are trying to do.

This part of the code is relevant.

$image->resize($value['width'], $value['height'], function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
})->sharpen(0)->save(public_path("$thmb_destination/$resizedName"), 100);

I must also note that the $constraint callback code is Intervention Image v2 code, and has no function with v3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants