Many times we face with the problem that how to resize, compress image or add a watermark to the image in php. With the help of this function you add watermark to image (optional feature). Here is a source code that will help you in resizing or adding watermark to your image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /** * Magic Resize Image function * @param string $source_image //Path of original image with image name and extension * @param string $destination //Destination Image * @param int $tn_w //Desired width * @param int $tn_h //Desired height * @param int $quality //Output quality of image 1 to 100, where 100 is best * @param string or boolean $wmsource //Waternmark image if available * @return boolean */ function resize($source_image, $destination, $tn_w, $tn_h, $quality = 100, $wmsource = false) { $info = getimagesize($source_image); $imgtype = image_type_to_mime_type($info[2]); #assuming the mime type is correct switch ($imgtype) { case 'image/jpeg': $source = imagecreatefromjpeg($source_image); break; case 'image/gif': $source = imagecreatefromgif($source_image); break; case 'image/png': $source = imagecreatefrompng($source_image); break; default: die('Invalid image type.'); } #Figure out the dimensions of the image and the dimensions of the desired thumbnail $src_w = imagesx($source); $src_h = imagesy($source); #Do some math to figure out which way we will need to crop the image #to get it proportional to the new size, then crop or adjust as needed $x_ratio = $tn_w / $src_w; $y_ratio = $tn_h / $src_h; if (($src_w <= $tn_w) && ($src_h <= $tn_h)) { //Orignal image is small from required $new_w = $src_w; $new_h = $src_h; } elseif (($x_ratio * $src_h) < $tn_h) { $new_h = ceil($x_ratio * $src_h); $new_w = $tn_w; } else { $new_w = ceil($y_ratio * $src_w); $new_h = $tn_h; } $newpic = imagecreatetruecolor(round($new_w), round($new_h)); imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h); $final = imagecreatetruecolor($tn_w, $tn_h); $backgroundColor = imagecolorallocate($final, 255, 255, 255); imagefill($final, 0, 0, $backgroundColor); //imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h); imagecopy($final, $newpic, (($tn_w - $new_w) / 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h); #if we need to add a watermark if ($wmsource) { #find out what type of image the watermark is $info = getimagesize($wmsource); $imgtype = image_type_to_mime_type($info[2]); #assuming the mime type is correct switch ($imgtype) { case 'image/jpeg': $watermark = imagecreatefromjpeg($wmsource); break; case 'image/gif': $watermark = imagecreatefromgif($wmsource); break; case 'image/png': $watermark = imagecreatefrompng($wmsource); break; default: die('Invalid watermark type.'); } #if we're adding a watermark, figure out the size of the watermark #and then place the watermark image on the bottom right of the image $wm_w = imagesx($watermark); $wm_h = imagesy($watermark); imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h); } if (imagejpeg($final, $destination, $quality)) { return true; } return false; } |