How to generate thumbnail from image and also apply watermark using php

Please refer below code for generate thumbnail from image and also apply watermark using php


createThumbnail('sign_in.png');function createThumbnail($filename) {

    $path_to_thumbs_directory = 'thumbs/';
    $mimeType = mime_content_type($filename);    if($mimeType=="image/png")    {        $im = imagecreatefrompng($filename);    }else if($mimeType == 'image/gif')    {        $im = imagecreatefromgif($filename);    }    else if($mimeType=='image/jpg' ||$mimeType=='image/jpeg')    {        $im = imagecreatefromjpeg($filename);    }
    $ox = imagesx($im);    $oy = imagesy($im);
    $nx = 256;    $ny = 256;
    $nm = imagecreatetruecolor($nx, $ny);
    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
    if(!file_exists($path_to_thumbs_directory)) {        if(!mkdir($path_to_thumbs_directory)) {            die("There was a problem. Please try again!");        }    }    $Watermark = 'logo2.png';    imagejpeg($nm, $path_to_thumbs_directory . $filename);    watermarkImage($path_to_thumbs_directory . $filename,$Watermark,$path_to_thumbs_directory . $filename,100);    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';    $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a      thumbnail has been created.';    echo $tn;}
function watermarkImage($SourceFile, $WaterMark, $DestinationFile=NULL, $opacity){    $padding = 10;    $a="'".$WaterMark."'";    $watermark = imagecreatefrompng($WaterMark);    $extenstion=substr($SourceFile,-4);    $mimeType = mime_content_type($SourceFile);    if($mimeType=="image/png")    {        $image = imagecreatefrompng($SourceFile);    }else if($mimeType == 'image/gif')    {        $image = imagecreatefromgif($SourceFile);    }    else if($mimeType=='image/jpg' ||$mimeType=='image/jpeg')    {        $image = imagecreatefromjpeg($SourceFile);    }

    $watermark_size = getimagesize($WaterMark);    $watermark_width = $watermark_size[0];    $watermark_height = $watermark_size[1];
    $image_size = getimagesize($SourceFile);
    $dest_x = $image_size[0] - $watermark_width - $padding;
    imagecopy($image, $watermark, $dest_x, 10, 0, 0, $watermark_width, $watermark_height);
    imagejpeg($image, $DestinationFile, 100);
    imagedestroy($image);    imagedestroy($watermark);}

Comments

Popular posts from this blog

How to add image option in nav menu in wordpress

How to add a custom email functionality in woocommerce,wordpress for example new email template for shipping order

How to change user id on checkout page for assign order to different user in woocommerce wordpress