本文實(shí)例講述了php實(shí)現(xiàn)水印文字和縮略圖的方法。分享給大家供大家參考,具體如下:
<?php  $im = 'xiatian.jpg';  $im2 = 'winter3.jpg';  $img = getImage($im);  $bimg = getImage($im2);  //$image = getimagesize($im);  //print_r($image);  //Array ( [0] => 150 width   /*[1] => 150 height   [2] => 3 圖片類型   [3] => width="150" height="150"   [bits] => 8   [mime] => image/png )*/  //圖片復(fù)制組合------$bimg----  imagecopy($bimg, $img, 300, 4, 0, 0, getWidth($im),getHeight($im));  //加水印文字  $color = imagecolorallocate($bimg,255, 255, 0);  $str = "中國(guó)";  $str = mb_convert_encoding($str, "html-entities","utf-8");  imagettftext($bimg, 42, 0, rand(20,getWidth($im2)-100), rand(40,getHeight($im2)), $color, 'MSYH.TTF', $str);  //剪切,重新生成縮略圖  $new = imagecreatetruecolor(50, 50);  imagecopyresized($new, $bimg, 0, 0, 0, 0, 50, 50, getWidth($im2), getHeight($im2));  //輸出到瀏覽器  header("Content-Type: image/jpeg");  //如果為png圖片也可正常顯示,瀏覽器負(fù)責(zé)正確解析  imagejpeg($new);  //生成大圖,小圖  imagejpeg($new,'small.jpg');  imagejpeg($bimg,'big.jpg');  function getImage($img_file) {    $image = getimagesize($img_file);    //判斷是那種圖片類型    switch($image[2]) {      case 1:        $img = @imagecreatefromgif($img_file);        break;      case 2:        $img = @imagecreatefromjpeg($img_file);        break;      case 3:        $img = @imagecreatefrompng($img_file);        break;    }    return $img;  }  function getWidth($img_file) {    $image = getimagesize($img_file);    return $image[0];  }  function getHeight($img_file) {    $image = getimagesize($img_file);    return $image[1];  }?>希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選