国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php上傳圖片并生成縮位圖代碼

2024-09-04 11:44:26
字體:
供稿:網(wǎng)友

php上傳圖片并生成縮位圖代碼,我們時常要上傳圖片,但也要保留自己的版權(quán)所以就會用到圖片加水印哦,下面的程序就是上傳圖片成功后再給圖片加上你自己做的水印效果,實例代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 滦南县| 海淀区| 称多县| 浮梁县| 香河县| 桃园市| 武宁县| 正阳县| 桓仁| 朝阳区| 紫阳县| 离岛区| 黄龙县| 皋兰县| 丰城市| 呼伦贝尔市| 霍邱县| 开平市| 崇文区| 邢台县| 洪湖市| 长沙市| 安溪县| 工布江达县| 基隆市| 柏乡县| 读书| 原阳县| 都匀市| 花莲县| 平顺县| 绥中县| 迁西县| 天等县| 临洮县| 奎屯市| 枝江市| 周至县| 大足县| 眉山市| 台中县|