03
04
Resize compress and Watermark To The Uploaded Image Using PHP

Today in this tutorial we showing how to upload, resize, compress and then add watermark to images using php. Resizing and compressing image sizes is always be the difficult task doing with the programming languages. But nowadays, php make this simple. With php you can do any type of image manipulation. For this GD library is been used. There are many other libraries which also used for image manipulation like Imagick, jQuery Plugins etc. In my previous post I had created similar image uploader script using jQuery and PHP, I suggest you go through that tutorial, because we will follow the same procedure here for image upload with progress bar, only thing we are going to add resize, compress and watermark to the uploaded images .

Resize compress watermark image using php

Adding watermark to image is one of the popular method to protect our own digital images from being copied by the third party in the website. It is to prevent the theft of images from internet thieves. Here in this we have used transparent image as watermark template, While uploading our image we merge it with watermark image to make our image safe.

Using PHP Imagecopy()

Imagecopy actually copies the defined portion of source image to the destination image, so we basically copy and merge the watermark image with the uploaded image. See the code below:

imagecopy($targetImage, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image

Resize, Compress and Watermark - PHP Script :

function compressAndWatermark($tempFile,  $newwidth, $newheight, $target, $ext){
   
$uploadTempFile = $tempFile;
$watermark_png_file = 'watermark.png';  // Path of the watermark image
list( $width, $height, $uploadType ) = getimagesize( $uploadTempFile );
if($ext=="jpg" || $ext=="jpeg" || $ext=="JPEG" || $ext=="JPG" )
{
$srcImage = imagecreatefromjpeg( $uploadTempFile );
$targetImage = imagecreatetruecolor( $newwidth, $newwidth );   
$k = 1;
}else if(($ext=="png")||($ext=="PNG")){
$srcImage = imagecreatefrompng( $uploadTempFile );
$targetImage = imagecreatetruecolor( $newwidth, $newwidth );  
$white = imagecolorallocate($targetImage,  255, 255, 255);  // To make the png images background white.
imagefilledrectangle($targetImage, 0, 0, $width, $height, $white); // Fill the background with white.
$k=2;
}else if($ext=="gif" || $ext=="GIF"){
$srcImage = imagecreatefromgif( $uploadTempFile );    
$targetImage = imagecreatetruecolor( $newwidth, $newwidth );  
$white = imagecolorallocate($targetImage,  255, 255, 255);
imagefilledrectangle($targetImage, 0, 0, $width, $height, $white);
$k=3;
}
imagecopyresampled( $targetImage, $srcImage, 0, 0, 0, 0, $newwidth, $newheight,$width, $height );
//Calculate the centre position of the images to watemark it.
    $watermark_left = ($newwidth/2)-(300/2); //watermark left
    $watermark_bottom = ($newheight/2)-(100/2); //watermark bottom
$watermark = imagecreatefrompng($watermark_png_file); //watermark image
// Use imagecopy() to merge two	 images
	imagecopy($targetImage, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image

if($k=1){
imagejpeg($targetImage, $target, 100 );
}
else if($k=2){
imagepng($targetImage, $target, 9 );
}else if($k=3){
imagepng($targetImage, $target );
}
// Free up the memory
imagedestroy($targetImage);
return $target;
}

You can download the file by click the link below. That's all!. All the best

By posted on - 3rd Apr 2016

Social Oauth Login

Personalized Map Navigation

Online Image Compression Tool

Image Compression

Advertisement

Recent Posts

Categories