php上传图片并生成缩位图代码

php上传图片并生成缩位图代码,我们时常要上传图片,但也要保留自己的版权所以就会用到图片加水印哦,下面的程序就是上传图片成功后再给图片加上你自己做的水印效果,实例代码如下:

  1. <?php
  2. class Image {
  3. var $imageResource = NULL;
  4. var $target = NULL;
  5. var $enableTypes = array();
  6. var $imageInfo = array();
  7. var $createFunc = '';
  8. var $imageType = NULL;
  9. /**
  10. * Construct for this class
  11. *
  12. * @param string $image
  13. * @return Image
  14. */
  15. function Image($image = NULL) {
  16. //get enables
  17. if(imagetypes() & IMG_GIF) {
  18. $this->enableTypes[] = 'image/gif';
  19. }
  20. if(imagetypes() & IMG_JPEG) {
  21. $this->enableTypes[] = 'image/jpeg';
  22. }
  23. if (imagetypes() & IMG_JPG) {
  24. $this->enableTypes[] = 'image/jpg';
  25. }
  26. if(imagetypes() & IMG_PNG) {
  27. $this->enableTypes[] = 'image/png';
  28. }
  29. //end get
  30. if($image != NULL) {
  31. $this->setImage($image);
  32. }
  33. }
  34. /**
  35. * set a image resource
  36. *
  37. * @param string $image
  38. * @return boolean
  39. */
  40. function setImage($image) {
  41. if(file_exists($image) && is_file($image)) {
  42. $this->imageInfo = getimagesize($image);
  43. $img_mime = strtolower($this->imageInfo['mime']);
  44. if(!in_array($img_mime, $this->enableTypes)) {
  45. exit('系统不能操作这种图片类型.');
  46. }
  47. switch ($img_mime) {
  48. case 'image/gif':
  49. $link = imagecreatefromgif($image);
  50. $this->createFunc = 'imagegif';
  51. $this->imageType = 'gif';
  52. break;
  53. case 'image/jpeg':
  54. case 'image/jpg':
  55. $link = imagecreatefromjpeg($image);
  56. $this->createFunc = 'imagejpeg';
  57. $this->imageType = 'jpeg';
  58. break;
  59. case 'image/png':
  60. $link = imagecreatefrompng($image);
  61. $this->createFunc = 'imagepng';
  62. $this->imageType = 'png';
  63. break;
  64. default:
  65. $link = 'unknow';
  66. $this->imageType = 'unknow';
  67. break;
  68. }
  69. if($link !== 'unknow') {
  70. $this->imageResource = $link;
  71. } else {
  72. exit('这种图片类型不能改变尺寸.');
  73. }
  74. unset($link);
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. /**
  81. * set header
  82. *
  83. */
  84. function setHeader() {
  85. switch ($this->imageType) {
  86. case 'gif':
  87. header('content-type:image/gif');
  88. break;
  89. case 'jpeg':
  90. header('content-type:image/jpeg');
  91. break;
  92. case 'png':
  93. header('content-type:image/png');
  94. break;
  95. default:
  96. exit('Can not set header.');
  97. break;
  98. }
  99. return true;
  100. }//开源代码phpfensi.com
  101. /**
  102. * change the image size
  103. *
  104. * @param int $width
  105. * @param int $height
  106. * @return boolean
  107. */
  108. function changeSize($width, $height = -1) {
  109. if(!is_resource($this->imageResource)) {
  110. exit('不能改变图片的尺寸,可能是你没有设置图片来源.');
  111. }
  112. $s_width = $this->imageInfo[0];
  113. $s_height = $this->imageInfo[1];
  114. $width = intval($width);
  115. $height = intval($height);
  116. if($width <= 0) exit('图片宽度必须大于零.');
  117. if($height <= 0) {
  118. $height = ($s_height / $s_width) * $width;
  119. }
  120. $this->target = imagecreatetruecolor($width, $height);
  121. if(@imagecopyresized($this->target, $this->imageResource, 0, 0, 0, 0, $width, $height, $s_width, $s_height))
  122. return true;
  123. else
  124. return false;
  125. }
  126. /**
  127. * Add watermark
  128. *
  129. * @param string $image
  130. * @param int $app
  131. */
  132. function addWatermark($image, $app = 50) {
  133. if(file_exists($image) && is_file($image)) {
  134. $s_info = getimagesize($image);
  135. } else {
  136. exit($image . '文件不存在.');
  137. }
  138. $r_width = $s_info[0];
  139. $r_height = $s_info[1];
  140. if($r_width > $this->imageInfo[0]) exit('水印图片必须小于目标图片');
  141. if($r_height > $this->imageInfo[1]) exit('水印图片必须小于目标图片');
  142. switch ($s_info['mime']) {
  143. case 'image/gif':
  144. $resource = imagecreatefromgif($image);
  145. break;
  146. case 'image/jpeg':
  147. case 'image/jpg':
  148. $resource = imagecreatefromjpeg($image);
  149. break;
  150. case 'image/png':
  151. $resource = imagecreatefrompng($image);
  152. break;
  153. default:
  154. exit($s_info['mime'] .'类型不能作为水印来源.');
  155. break;
  156. }
  157. $this->target = &$this->imageResource;
  158. imagecopymerge($this->target, $resource, $this->imageInfo[0] - $r_width - 5, $this->imageInfo[1] - $r_height - 5, 0,0 ,$r_width, $r_height, $app);
  159. imagedestroy($resource);
  160. unset($resource);
  161. }
  162. /**
  163. * create image
  164. *
  165. * @param string $name
  166. * @return boolean
  167. */
  168. function create($name = NULL) {
  169. $function = $this->createFunc;
  170. if($this->target != NULL && is_resource($this->target)) {
  171. if($name != NULL) {
  172. $function($this->target, $name);
  173. } else {
  174. $function($this->target);
  175. }
  176. return true;
  177. } else if($this->imageResource != NULL && is_resource($this->imageResource)) {
  178. if($name != NULL) {
  179. $function($this->imageResource, $name);
  180. } else {
  181. $function($this->imageResource);
  182. }
  183. return true;
  184. } else {
  185. exit('不能创建图片,原因可能是没有设置图片来源.');
  186. }
  187. }
  188. /**
  189. * free resource
  190. *
  191. */
  192. function free() {
  193. if(is_resource($this->imageResource)) {
  194. @imagedestroy($this->imageResource);
  195. }
  196. if(is_resource($this->target)) {
  197. @imagedestroy($this->target);
  198. }
  199. }
  200. }
  201. ?>