Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 75
  • Last Modified:

Creating Image Thumbnails Using PHP

Do you have a good example of how to Create Image Thumbnails Using PHP?
thank you
0
Braveheartli
Asked:
Braveheartli
1 Solution
 
Julian HansenCommented:
From this article https://davidwalsh.name/create-image-thumbnail-php
function make_thumb($src, $dest, $desired_width) {

	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height * ($desired_width / $width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
	
	/* copy source image at a resized size */
	imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
	
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image, $dest);
}

Open in new window

0
 
Ray PaseurCommented:
Here is my teaching example...
<?php // demo/image_thumbnail.php
/**
 * RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE
 * USE CASE: <img src="image_thumbnail.php?i=your.jpg&w=150&h=100" />
 */
error_reporting(E_ALL);


// ACQUIRE THE ARGUMENTS - MAY WANT SOME SANITY TESTS HERE?
$thumb_w   = !empty($_GET["w"]) ? $_GET['w'] : 600;
$thumb_h   = !empty($_GET["h"]) ? $_GET['h'] : 400;
$image_url = !empty($_GET["i"]) ? $_GET['i'] : trigger_error("Missing GET argument 'i' for Image URL", E_USER_ERROR);;

// CREATE THE THUMBNAIL IMAGE RESOURCE AND FILL IN TRANSPARENT
$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
imagesavealpha($thumb, TRUE);
$empty = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($thumb, 0, 0, $empty);

// GET ORIGINAL IMAGE DIMENSIONS
$array = getimagesize($image_url);
if ($array)
{
    list($image_w, $image_h) = $array;
}
else
{
    trigger_error("NO IMAGE $image_url", E_USER_ERROR);
}

// ACQUIRE THE ORIGINAL IMAGE
$image_ext = explode('.', $image_url);
$image_ext = end($image_ext);
$image_ext = trim(strtoupper($image_ext));
switch($image_ext)
{
    case 'JPG' :
    case 'JPEG' :
        $image = @imagecreatefromjpeg($image_url);
        if ($image) break;

    case 'PNG' :
        $image = @imagecreatefrompng($image_url);
        if ($image) break;

    default : trigger_error("UNKNOWN IMAGE TYPE: $image_url", E_USER_ERROR);
}

// GET THE LESSER OF THE RATIO OF THUMBNAIL H OR W DIMENSIONS
$ratio_w = ($thumb_w / $image_w);
$ratio_h = ($thumb_h / $image_h);
$ratio   = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;

// COMPUTE THUMBNAIL IMAGE DIMENSIONS
$thumb_w_resize = $image_w * $ratio;
$thumb_h_resize = $image_h * $ratio;

// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$thumb_w_offset = ($thumb_w - $thumb_w_resize) / 2.0;
$thumb_h_offset = ($thumb_h - $thumb_h_resize) / 2.0;

// COPY THE IMAGE TO THE CENTER OF THE THUMBNAIL
imagecopyresampled
( $thumb
, $image
, $thumb_w_offset
, $thumb_h_offset
, 0
, 0
, $thumb_w_resize
, $thumb_h_resize
, $image_w
, $image_h
)
;

// SHOW THE NEW THUMB IMAGE
header('Content-type: image/png');
imagepng($thumb);

// RELEASE THE MEMORY USED BY THE RESOURCES
imagedestroy($thumb);
imagedestroy($image);

Open in new window

Here is a similar teaching example, showing how to resize and crop an image.  You can see it in  action on my server, here:
https://iconoun.com/demo/image_resize_and_crop.php
<?php // demo/image_resize_and_crop.php
/**
 * Resize an image proportionally
 * Crop to specified size
 *
 * https://www.experts-exchange.com/questions/28982826/resizeing-PHP-image.html
 */
error_reporting(E_ALL);

function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality=75)
{
    // ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
    $original = imagecreatefromjpeg($original_image_url);
    if (!$original) return FALSE;

    // GET ORIGINAL IMAGE DIMENSIONS
    list($original_w, $original_h) = getimagesize($original_image_url);

    // RESIZE IMAGE AND PRESERVE PROPORTIONS
    $thumb_w_resize = $thumb_w;
    $thumb_h_resize = $thumb_h;
    if ($original_w > $original_h)
    {
        $thumb_h_ratio  = $thumb_h / $original_h;
        $thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
    }
    else
    {
        $thumb_w_ratio  = $thumb_w / $original_w;
        $thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
    }
    if ($thumb_w_resize < $thumb_w)
    {
        $thumb_h_ratio  = $thumb_w / $thumb_w_resize;
        $thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
        $thumb_w_resize = $thumb_w;
    }

    // CREATE THE PROPORTIONAL IMAGE RESOURCE
    $thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
    if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;

    // ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
    // imagejpeg($thumb, 'thumbs/temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);

    // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
    $final = imagecreatetruecolor($thumb_w, $thumb_h);

    $thumb_w_offset = 0;
    $thumb_h_offset = 0;
    if ($thumb_w < $thumb_w_resize)
    {
        $thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
    }
    else
    {
        $thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
    }

    if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;

    // STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
    if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
    return TRUE;
}


// USE CASE
echo '<a target="_blank" href="images/image_600x374.jpg">Original 600x374</a><br/>';

resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_100x100.jpg', 100, 100);
echo '<a target="_blank" href="thumbs/temp_100x100.jpg">100x100</a><br/>';

resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_200x100.jpg', 200, 100);
echo '<a target="_blank" href="thumbs/temp_200x100.jpg">200x100</a><br/>';

resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_200x300.jpg', 200, 300);
echo '<a target="_blank" href="thumbs/temp_200x300.jpg">200x300</a><br/>';

Open in new window

1
 
BraveheartliMarketingAuthor Commented:
Thank you so much Ray Paseur.

You are the really great. Thank you so much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now