上传图片为了推扩网站我们可能会在图片上增加水印,或对图片进行缩小,下面提供的代码就有这个功能,php 加水印、缩略图的实现代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
- function watermark($bigimg, $smallimg, $coord = 1){
-
-
- $bi = getimagesize($bigimg);
- switch($bi[2]){
- case 1:
- $im1 = imagecreatefromgif($bigimg);break;
- case 2;
- $im1 = imagecreatefromjpeg($bigimg);break;
- case 3;
- $im1 = imagecreatefrompng($bigimg);break;
- }
- $si = getimagesize($smallimg);
- switch($si[2]){
- case 1:
- $im2 = imagecreatefromgif($smallimg);break;
- case 2;
- $im2 = imagecreatefromjpeg($smallimg);break;
- case 3;
- $im2 = imagecreatefrompng($smallimg);break;
- }
-
- switch($coord){
- case 1:
- imagecopy ( $im1, $im2, 0, 0, 0, 0, $si[0], $si[1] ); break;
- case 2:
- imagecopy ( $im1, $im2, $bi[0]-$si[0], 0, 0, 0, $si[0], $si[1] ); break;
- case 3:
- imagecopy ( $im1, $im2, $bi[0]-$si[0], $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break;
- case 4:
- imagecopy ( $im1, $im2, 0, $bi[1]-$si[1], 0, 0, $si[0], $si[1] ); break;
- case 5:
- imagecopy ( $im1, $im2, ($bi[0]-$si[0])/2, ($bi[1]-$si[1])/2, 0, 0, $si[0], $si[1] ); break;
- }
-
- switch($bi[2]){
- case 1:
- imagegif($im1);break;
- case 2;
- imagejpeg($im1);break;
- case 3;
- imagepng($im1);break;
- }
- imagedestroy($im1);
- }
-
-
-
-
-
-
-
-
-
- function thumbnail($srcimg, $multiple = 2){
-
- $srcimg_arr = getimagesize($srcimg);
-
- $thumb_width = $srcimg_arr[0] / $multiple;
- $thumb_height = $srcimg_arr[1] / $multiple;
-
- switch($srcimg_arr[2]){
- case 1:
- $im = imagecreatefromgif($srcimg);break;
- case 2;
- $im = imagecreatefromjpeg($srcimg);break;
- case 3;
- $im = imagecreatefrompng($srcimg);break;
- }
-
- $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
- imagecopyresized($thumb, $im, 0, 0, 0 ,0, $thumb_width, $thumb_height, $srcimg_arr[0], $srcimg_arr[1]);
- switch($srcimg_arr[2]){
- case 1:
- imagegif($thumb); break;
- case 2;
- imagejpeg($thumb); break;
- case 3;
- imagepng($thumb); break;
- }
- imagepng($thumb);
- imagedestroy($thumb);
- }
-
-
- thumbnail('abc.png',3);
- ?>
|