Image sizing using imagick

I was wondering if imagick can be used to dynamically resize images. For example the home page shows a list of posts with an image, i wanted those images to be a certain size, then when the user clicks on one of the posts they will get a larger image that would fit to their screen size. Another one would be in the admin section that shows the posts the image would ideally be more thumbnail size. Currently the image only shows one size everywhere it is displayed. Or would this be more of a css problem? Thank you

Most images that you deal with for web pages are compressed (jpg, gif, png.) To resize them, they must be uncompressed into a raw bitmap format. This requires a lot of memory and processing. So, no you would not dynamically resize images. You would produce a one-time thumbnail image and save it, then either use the thumbnail or the full size image file on a web page.

1 Like

When it comes to file handling in PHP, specifically for image manipulation, the Intervention Image library stands out as an exceptional choice. This library, constructed atop the foundations of the GD Library or the Imagick PHP extension, provides a user-friendly and efficient way to deal with images. It excels in offering an interface that simplifies complex operations, enhancing security and efficiency in the process. Hence, for any developer seeking to streamline their image handling procedures in PHP, the Intervention Image library is a highly recommended tool to consider.

Here’s a portion of the code showing it in use →

    /*
     * Create unique name for image.
     */
    $image_random_string = bin2hex(random_bytes(16));
    $image_path = 'assets/image_path/img-gallery-' . $image_random_string . '-2048x1365' . '.' . $file_ext;
    $thumb_path = 'assets/thumb_path/thumb-gallery-' . $image_random_string . '-205x137' . '.' . $file_ext;


    move_uploaded_file($file_tmp, $image_path);
    move_uploaded_file($thumb_tmp, $thumb_path);


    // Load the image
    $image = Image::make($image_path);

    // Resize the image
    $image->resize(2048, 1365, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });

   // Save the new image
    $image->save($image_path, 100);

    // Load the image with Intervention Image
    $image = Image::make($image_path);

    // Resize the image while maintaining the aspect ratio
    $image->resize(205, 137, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });

    // Save the thumbnail
    $image->save($thumb_path, 100);
    

    $data['image_path'] = $image_path;
    $data['thumb_path'] = $thumb_path;

It can be a little tricky to install the Library, but in my opinion it is worth it.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service