Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:10+00:00 2026-05-22T20:49:10+00:00

What PHP library would you recommend for thumbnails generation? I need it to work

  • 0

What PHP library would you recommend for thumbnails generation?
I need it to work with GD (not only Imagick), be able to stretch images or keep aspect ratio when only one dimension given, and – most important – be able to crop&resize image (when generating thumbnails, I need all of them to be, let’s say 128×128, so I want the library to crop images in that case)
What are yours recommendations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:49:11+00:00Added an answer on May 22, 2026 at 8:49 pm

    I coded my own GD wrapper for my framework, phunction. Some examples:

    $input = '/path/to/source/image.jpg';
    $output = '/path/to/destination/image.jpg';
    
    /*
    this will crop the biggest possible square (since 128/128 = 1)
    from the center of the image and resize the width to 500px
    while keeping the height porportional (to maintian aspect ratio)
    */
    ph()->Disk->Image($input, '128/128', '500*0', null, $output);
    
    /*
    same as the above, but aspect ratio is not respected
    since both width and height are specified
    */
    ph()->Disk->Image($input, '128/128', '500*1000', null, $output);
    
    /*
    no cropping, just porpotional resize to the width
    */
    ph()->Disk->Image($input, null, '500*0', null, $output);
    

    There are other good alternatives like Asido and WideImage but this one works for me, and since it is a simple method with only one dependency you can easily use it standalone:

    function Image($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
    {
        if (isset($input, $output) === true)
        {
            if (is_string($input) === true)
            {
                $input = @ImageCreateFromString(@file_get_contents($input));
            }
    
            if (is_resource($input) === true)
            {
                $size = array(ImageSX($input), ImageSY($input));
                $crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
                $scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));
    
                if (count($crop) == 2)
                {
                    $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);
    
                    if ($crop[0] > $crop[1])
                    {
                        $size[0] = round($size[1] * $crop[1]);
                    }
    
                    else if ($crop[0] < $crop[1])
                    {
                        $size[1] = round($size[0] / $crop[1]);
                    }
    
                    $crop = array(ImageSX($input) - $size[0], ImageSY($input) - $size[1]);
                }
    
                else
                {
                    $crop = array(0, 0);
                }
    
                if (count($scale) >= 1)
                {
                    if (empty($scale[0]) === true)
                    {
                        $scale[0] = round($scale[1] * $size[0] / $size[1]);
                    }
    
                    else if (empty($scale[1]) === true)
                    {
                        $scale[1] = round($scale[0] * $size[1] / $size[0]);
                    }
                }
    
                else
                {
                    $scale = array($size[0], $size[1]);
                }
    
                $image = ImageCreateTrueColor($scale[0], $scale[1]);
    
                if (is_resource($image) === true)
                {
                    ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
                    ImageSaveAlpha($image, true);
                    ImageAlphaBlending($image, true);
    
                    if (ImageCopyResampled($image, $input, 0, 0, round($crop[0] / 2), round($crop[1] / 2), $scale[0], $scale[1], $size[0], $size[1]) === true)
                    {
                        $result = false;
    
                        if ((empty($sharp) !== true) && (is_array($matrix = array_fill(0, 9, -1)) === true))
                        {
                            array_splice($matrix, 4, 1, (is_int($sharp) === true) ? $sharp : 16);
    
                            if (function_exists('ImageConvolution') === true)
                            {
                                ImageConvolution($image, array_chunk($matrix, 3), array_sum($matrix), 0);
                            }
                        }
    
                        if ((isset($merge) === true) && (is_resource($merge = @ImageCreateFromString(@file_get_contents($merge))) === true))
                        {
                            ImageCopy($image, $merge, round(0.95 * $scale[0] - ImageSX($merge)), round(0.95 * $scale[1] - ImageSY($merge)), 0, 0, ImageSX($merge), ImageSY($merge));
                        }
    
                        foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
                        {
                            if (preg_match('~' . $key . '$~i', $output) > 0)
                            {
                                $type = str_replace('?', '', $key);
                                $output = preg_replace('~^[.]?' . $key . '$~i', '', $output);
    
                                if (empty($output) === true)
                                {
                                    header('Content-Type: image/' . $type);
                                }
    
                                $result = call_user_func_array('Image' . $type, array($image, $output, $value));
                            }
                        }
    
                        return $result;
                    }
                }
            }
        }
    
        else if (count($result = @GetImageSize($input)) >= 2)
        {
            return array_map('intval', array_slice($result, 0, 2));
        }
    
        return false;
    }
    

    Watermarking and converting / displaying (instead of saving) images is also supported.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question What PHP library would you recommend for attaching meeting notices to email? Preference
Is PHP's GD library suitable for drawing images from scratch? Or would I be
Is there a PHP class/library that would allow me to query an XHTML document
Why would I want to use PHP's filter library? Why wouldn't I? It seems
Is there a PHP library (or maybe a snippet) which would cover the following
I am developing my own PHP Library and I would like to call RESTful
I need a PHP library/script that can retreive data from feeds, no matter if
I want to control the display of images with the PHP GD library --
I need to integrate an event dispatcher in my own codebase (custom PHP library),
Which is the best PHP library for openID integration?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.