DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: Php

PHP thumb creation and image merge

Share

PHP thumb uses the GD library to create thumbnails from images (jpeg, gif, png etc) on run time.

You can configure the output size and source will be entire image or a portion of original image.

PHP uses imagejpeg function which output image to browser or file.

Syntax:

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

Return Values:

Returns TRUE on success and FALSE on failure.

Below are the two functions for thumbnail creation and merging two images.

Thumb creation:

<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbHeight, $fname )
{
$info = pathinfo($pathToImages . $fname);

if ( strtolower($info['extension']) == 'jpg' )
$img = imagecreatefromjpeg( "{$pathToImages}" );
if ( strtolower($info['extension']) == 'gif' )
$img = imagecreatefromgif( "{$pathToImages}" );
if ( strtolower($info['extension']) == 'png' )
$img = imagecreatefrompng( "{$pathToImages}" );

$width = imagesx( $img );
$height = imagesy( $img );

$new_height = $thumbHeight;
$new_width = floor( $width * ( $new_height / $height ) );

$tmp_img = imagecreatetruecolor( $new_width, $new_height );

imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

if ( strtolower($info['extension']) == 'jpg' )
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
if ( strtolower($info['extension']) == 'gif' )
imagegif( $tmp_img, "{$pathToThumbs}{$fname}" );
if ( strtolower($info['extension']) == 'png' )
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );
}
?>

Read Also: Read Gmail Emails using PHP & IMAP

Image merging:

<?php
function imagemerge($image1,$image2,$sizeW,$sizeH,$targetfile)
{
$image="images/white.jpg";
list($width1, $height1) = getimagesize($image1);
list($width2, $height2) = getimagesize($image2);

$aW1 = $width1;
$aH1 = $height1;

$aW2 = $width2;
$aH2 = $height2;

$info1 = pathinfo($image1);
$info2 = pathinfo($image2);

if ( strtolower($info1['extension']) == 'jpg' )
$photo = imagecreatefromjpeg($image1);
if ( strtolower($info1['extension']) == 'gif' )
$photo = imagecreatefromgif($image1);
if ( strtolower($info1['extension']) == 'png' )
$photo = imagecreatefrompng($image1);

if ( strtolower($info2['extension']) == 'jpg' )
$photo2 = imagecreatefromjpeg($image2);
if ( strtolower($info2['extension']) == 'gif' )
$photo2 = imagecreatefromgif($image2);
if ( strtolower($info2['extension']) == 'png' )
$photo2 = imagecreatefrompng($image2);

$photoFrame = imagecreatefromjpeg($image);
$fx1=($sizeW-$aW1)/2;
$fy1=($sizeH-$aH1)/2;

$fx2=$sizeW+($sizeW-$aW2)/2;
$fy2=($sizeH-$aH2)/2;

imagecopyresampled($photoFrame, $photo, $fx1, $fy1, 0, 0, $aW1, $aH1, $aW1, $aH1);
imagecopyresampled($photoFrame, $photo2, $fx2, $fy2, 0, 0, $aW2, $aH2, $aW2, $aH2);
imagejpeg($photoFrame, $targetfile);
}
?>
Namaste UI

For any types of queries, you can contact us on info[at]namasteui.com.

View Comments

    • Hi Riyaz,
      You need to call the function "imagemerge" with all parameters.
      Create a folder "images" in the same path and put an image called "white.jpg" just like "images/white.jpg".
      Then call the function:
      imagemerge('sample-1.jpg','sample-2.jpg',400,400,'final.jpg');
      You need to adjust the height and width as per white image.
      Thanks.

  • Sir I was try this with all parameters but its not work for me can you create this functions than send me a zip file
    Thanks you....

    • You need to put all there images in your file path.
      1) first image. 2) second image and 3) a simple white.jpg in images folder.
      Then call the function as below:
      imagemerge(‘sample-1.jpg’,’sample-2.jpg’,400,400,’final.jpg’);
      Thanks.

Recent Posts

Your Complete Guide to the 461 Visa: Building a Life in Australia with Your New Zealand Partner

People usually stumble onto the 461 visa when they realise there’s no simple way to…

4 days ago

10 Buyer Personas You Encounter in Your Retail Store

When you run a brick-and-mortar retail store, you encounter an array of customer personalities. From…

4 days ago

Why Roarbank Is Transforming Everyday Banking in India

In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…

4 days ago

Quality with Quantity: Importing LED Lights Can Save You Big Bucks

If you are planning to start a lighting business and looking for a supplier of…

5 days ago

11 Reasons Why Avoiding Plagiarism is Necessary for SEO and content marketing

If you are a copywriter or a content marketer, you will second my statement that…

1 week ago

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

2 weeks ago