php產生縮圖,加浮水印種

來源:互聯網
上載者:User
php產生縮圖,加浮水印類

這個一個簡單的GD庫操作

 $dinfo['height'] || $winfo['width'] > $dinfo['width']) {return false;}// 兩張圖,讀到畫布上! 但是圖片可能是png,可能是jpeg,用什麼函數讀?$dfunc = 'imagecreatefrom' . $dinfo['ext'];$wfunc = 'imagecreatefrom' . $winfo['ext'];if (!function_exists($dfunc) || !function_exists($wfunc)) {return false;}// 動態載入函數來建立畫布$dim = $dfunc($dst);// 建立待操作的畫布$wim = $wfunc($water);// 建立浮水印畫布// 根據浮水印的位置 計算粘貼的座標switch($pos) {case 0 :// 左上方$posx = 0;$posy = 0;break;case 1 :// 右上方$posx = $dinfo['width'] - $winfo['width'];$posy = 0;break;case 3 :// 左下角$posx = 0;$posy = $dinfo['height'] - $winfo['height'];break;default :$posx = $dinfo['width'] - $winfo['width'];$posy = $dinfo['height'] - $winfo['height'];}// 加浮水印imagecopymerge($dim, $wim, $posx, $posy, 0, 0, $winfo['width'], $winfo['height'], $alpha);// 儲存if (!$save) {$save = $dst;unlink($dst);// 刪除原圖}$createfunc = 'image' . $dinfo['ext'];$createfunc($dim, $save);imagedestroy($dim);imagedestroy($wim);return true;}/** thumb 產生縮圖 等比例縮放,兩邊留白 **/public static function thumb($dst, $save = NULL, $width = 200, $height = 200) {// 首先判斷待處理的圖片存不存在$dinfo = self::imageInfo($dst);if ($dinfo == false) {return false;}// 計算縮放比例$calc = min($width / $dinfo['width'], $height / $dinfo['height']);// 建立原始圖的畫布$dfunc = 'imagecreatefrom' . $dinfo['ext'];$dim = $dfunc($dst);// 建立縮減畫布$tim = imagecreatetruecolor($width, $height);// 建立白色填充縮減畫布$white = imagecolorallocate($tim, 255, 255, 255);// 填充縮減畫布imagefill($tim, 0, 0, $white);// 複製並縮減$dwidth = (int)$dinfo['width'] * $calc;$dheight = (int)$dinfo['height'] * $calc;$paddingx = (int)($width - $dwidth) / 2;$paddingy = (int)($height - $dheight) / 2;imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo['width'], $dinfo['height']);// 儲存圖片if (!$save) {$save = $dst;unlink($dst);}$createfunc = 'image' . $dinfo['ext'];$createfunc($tim, $save);imagedestroy($dim);imagedestroy($tim);return true;}}// print_r(ImageTool::imageInfo('./home.jpg'));/* echo ImageTool::water('./home.jpg','./smallfeng.png','home1.jpg',0)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home2.jpg',1)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home3.jpg',2)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home4.jpg',3)?'OK':'FAIL'; */


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.