最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP 處理圖片的類實(shí)現(xiàn)代碼

 更新時(shí)間:2009年10月23日 21:35:58   作者:  
PHP 處理圖片的類實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

<?php
/**
* author:yagas
* email:yagas60@21cn.com
*/
class Image
{
/** 類保護(hù)變量 */
protected $th_width = 100;
protected $th_height = 50;
protected $quality = 85; //圖片質(zhì)量
protected $transparent = 50; //水印透明度
protected $background = "255,255,255"; //背景顏色
/**
* 生成縮略圖文件
* @param $src 原圖文件
* @param $dst 目標(biāo)文件
*/
public function thumb($src, $dst=null, $output=true)
{
$thumb = array($this->th_width, $this->th_height);
$this->scale($src, $thumb, $dst, $output);
}
/**
* 對圖片按百分比進(jìn)行縮放處理
* @param string $src 原圖文件
* @param string $dst 輸入的目標(biāo)文件
* @param float/array $zoom 縮放比例,浮點(diǎn)類型時(shí)按百分比綻放,數(shù)組類型時(shí)按指定大小時(shí)行縮放
* @param boolean $output 是否生成文件輸出
*/
public function scale($src, $dst=null, $zoom=1, $output=true)
{
if(!file_exists($src)) die('File not exists.');
if(!$zoom) die('the zoom undefine.');
$src_im = $this->IM($src);
$old_width = imagesx($src_im);
if(is_float($zoom)) {
//按百分比進(jìn)行縮放
$new_width = $old_width * $zoom;
}
elseif(is_array($zoom)) {
//明確的縮放尺寸
$new_width = $zoom[0];
}
//是否定義的縮放的高度
if(!isset($zoom[1])) {
//等比例縮放
$resize_im = $this->imageresize($src_im, $new_width);
}
else {
//非等比例縮放
$resize_im = $this->imageresize($src_im, $new_width, $zoom[1]);
}
if(!$output) {
header("Content-type: image/jpeg");
imagejpeg($resize_im, null, $this->quality);
}
else {
$new_file = empty($dst)? $src:$dst;
imagejpeg($resize_im, $new_file, $this->quality);
}
imagedestroy($im);
imagedestroy($nIm);
}
/**
* 對圖片進(jìn)行裁切
* @param $src 原始文件
* @param $dst 目標(biāo)文件
* @param $output 是否生成目標(biāo)文件
*/
public function capture($src, $dst=null, $output=true) {
if(!file_exists($src)) die('File not exists.');
$width = $this->th_width;
$height = $this->th_height;
$src_im = $this->IM($src);
$old_width = imagesx($src_im);
$old_height = imagesy($src_im);
$capture = imagecreatetruecolor($width, $height);
$rgb = explode(",", $this->background);
$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]);
imagefill($capture, 0, 0, $white);
//當(dāng)圖片大于縮略圖時(shí)進(jìn)行縮放
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize($src_im, $width);
//圖片比例不合規(guī)范時(shí),重新計(jì)算比例進(jìn)行裁切
if(imagesy($resize_im) < $height) {
$proportion = $old_height/$this->th_height;
$resize_im = $this->imageresize($src_im, $old_width/$proportion);
}
$posx = 0;
$posy = 0;
}
else {
//圖片小于縮略圖時(shí)將圖片居中顯示
$posx = ($width-$old_width)/2;
$posy = ($height-$old_height)/2;
$resize_im = $src_im;
}
imagecopy($capture, $resize_im, $posx, $posy, 0, 0, imagesx($resize_im), imagesy($resize_im));
if(!$output) {
header("Content-type: image/jpeg");
imagejpeg($capture, null, $this->quality);
}
else {
$new_file = empty($dst)? $src:$dst;
imagejpeg($capture, $new_file, $this->quality);
}
imagedestroy($src_im);
@imagedestroy($resize_im);
imagedestroy($capture);
}
/**
* 寫入水印圖片
* @param $src 需要寫入水印的圖片
* @param $mark 水印圖片
* @param $transparent 水印透明度
*/
public function mark($src, $mark, $dst='', $output=true)
{
$mark_info = getimagesize($mark);
$src_info = getimagesize($src);
list($mw,$mh) = $mark_info;
list($sw,$sh) = $src_info;
$px = $sw - $mw;
$py = $sh - $mh;
$im = $this->IM($src);
$mim = $this->IM($mark);
imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh, $this->transparent);
if($output){
$new_file = empty($dst)? $src:$dst;
imagejpeg($im, $new_file, $this->quality);
}
else
{
header('Content-type: image/jpeg');
imagejpeg($im);
}
imagedestroy($im);
imagedestroy($mim);
}
/**
* 通過文件,獲取不同的GD對象
*/
protected function IM($file)
{
if(!file_exists($file)) die('File not exists.');
$info = getimagesize($file);
switch($info['mime'])
{
case 'image/gif':
$mim = imagecreatefromgif($file);
break;
case 'image/png':
$mim = imagecreatefrompng($file);
imagealphablending($mim, false);
imagesavealpha($mim, true);
break;
case 'image/jpeg':
$mim = imagecreatefromjpeg($file);
break;
default:
die('File format errors.');
}
return $mim;
}
/**
* 對圖片進(jìn)行縮放的處理
* @param resource $src_im 圖像GD對象
* @param integer $width 圖片的寬度
* @param integer $height 圖片的高度,如果不設(shè)置高度,將對圖片進(jìn)行等比例縮放
* @return resuorce $im 返回一個(gè)GD對象
*/
protected function imageresize($src_im, $width, $height=null) {
$old_width = imagesx($src_im);
$old_height = imagesy($src_im);
$proportion = $old_width/$old_height;
$new_width = $width;
$new_height = is_null($height)? round($new_width / $proportion):$height;
//創(chuàng)建新的圖象并填充默認(rèn)的背景色
$im = imagecreatetruecolor($new_width, $new_height);
$rgb = explode(",", $this->background);
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagefill($im, 0, 0, $white);
//對圖片進(jìn)行縮放
imagecopyresized($im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
return $im;
}
/**
* 類變量賦值
*/
public function __set($key, $value)
{
$this->$key = $value;
}
/**
* 獲取類變量值
*/
public function __get($key)
{
return $this->$key;
}
}
?>

相關(guān)文章

  • php多重接口的實(shí)現(xiàn)方法

    php多重接口的實(shí)現(xiàn)方法

    這篇文章主要介紹了php多重接口的實(shí)現(xiàn)方法,實(shí)例分析了php多重接口的定義與使用技巧,需要的朋友可以參考下
    2015-06-06
  • PHP數(shù)組基本用法與知識點(diǎn)總結(jié)

    PHP數(shù)組基本用法與知識點(diǎn)總結(jié)

    這篇文章主要介紹了PHP數(shù)組基本用法與知識點(diǎn),總結(jié)整理了PHP數(shù)組基本概念、用法、定義、訪問、刪除等相關(guān)操作技巧,需要的朋友可以參考下
    2020-06-06
  • 20個(gè)PHP常用類庫小結(jié)

    20個(gè)PHP常用類庫小結(jié)

    下面是一些非常有用的PHP類庫,相信一定可以為你的WEB開發(fā)提供更好和更為快速的方法。
    2011-09-09
  • 詳細(xì)分析PHP 命名空間(namespace)

    詳細(xì)分析PHP 命名空間(namespace)

    這篇文章主要介紹了PHP 命名空間(namespace)的的相關(guān)資料,文中講解非常詳細(xì),實(shí)例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 服務(wù)器變量 $_SERVER 的深入解析

    服務(wù)器變量 $_SERVER 的深入解析

    本篇文章是對服務(wù)器變量$_SERVER進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • Windows上PHP安裝redis擴(kuò)展和igbinary擴(kuò)展

    Windows上PHP安裝redis擴(kuò)展和igbinary擴(kuò)展

    php擴(kuò)展就是php核心并不支持的功能,通過安裝擴(kuò)展增加PHP的功能,在Windows上有兩種加載PHP擴(kuò)展的方式:把擴(kuò)展編譯進(jìn)?PHP,或者加載?DLL,加載預(yù)編譯的擴(kuò)展是更簡單更被推薦的方式,要加載某擴(kuò)展,需要在系統(tǒng)中有其相對應(yīng)的“.dll”文件
    2023-10-10
  • PHP記錄頁面停留時(shí)間的方法

    PHP記錄頁面停留時(shí)間的方法

    這篇文章主要介紹了PHP記錄頁面停留時(shí)間的方法,涉及PHP結(jié)合js針對文件與時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下
    2016-03-03
  • phpinfo()中Loaded Configuration File(none)的解決方法

    phpinfo()中Loaded Configuration File(none)的解決方法

    這篇文章主要給大家介紹了phpinfo()中Loaded Configuration File(none)問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • PHP設(shè)計(jì)模式 注冊表模式

    PHP設(shè)計(jì)模式 注冊表模式

    注冊表模式其實(shí)是一個(gè)單例模式,注冊表類提供靜態(tài)方法(或單例對象的實(shí)例化方法)來讓其它對象訪問其中的數(shù)據(jù)(通常是對象)。整個(gè)系統(tǒng)中的每個(gè)對象都可以訪問這些數(shù)據(jù)對象
    2012-02-02
  • PHP中創(chuàng)建和驗(yàn)證哈希的簡單方法實(shí)探

    PHP中創(chuàng)建和驗(yàn)證哈希的簡單方法實(shí)探

    這篇文章主要介紹了PHP中創(chuàng)建和驗(yàn)證哈希的簡單方法,即為Password Hashing API的使用介紹,需要的朋友可以參考下
    2015-07-07

最新評論

巩义市| 宜黄县| 临沭县| 太湖县| 泽库县| 嘉鱼县| 体育| 临江市| 丹东市| 土默特左旗| 巴青县| 广水市| 昌都县| 永清县| 浦北县| 千阳县| 蚌埠市| 英吉沙县| 大埔县| 衢州市| 达州市| 黎川县| 海盐县| 忻州市| 集贤县| 宽城| 金沙县| 大竹县| 静海县| 金寨县| 高平市| 洪洞县| 临邑县| 宜宾县| 巴东县| 龙泉市| 托里县| 景德镇市| 嵊泗县| 青阳县| 沙田区|