찾다
php教程php手册php图片处理类(生成缩略图,增加水印,获取图片信息)

php图片处理类(生成缩略图,增加水印,获取图片信息)

Jun 13, 2016 am 09:48 AM
php정보그림증가하다다루다지도 시간양수표생성하다친절한얻다

php教程图片处理类(生成缩略图,增加水印,获取图片信息)
本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/

class image
{
 public $info=array();

 function __construct()
 {
  !extension_loaded('gd') && exit("www.bKjia.c0m提示:服务器环境不支持gd库");
  return true;
 }

 function image()
 {
  $this->__construct();
 }
 
 function thumb($image,$thumb_width=300,$thumb_height=225)
 {
  $info=$this->info($image);
  $scale=min(1,min($thumb_width/$info['width'],$thumb_height/$info['height'])); //按比例缩放
  $thumb_width=intval($info['width']*$scale);
  $thumb_height=intval($info['height']*$scale);
  $createfunc='imagecreatefrom'.($info['type']=='jpg'?'jpeg':$info['type']);
  $im=$createfunc($image);
  $thumb_im=$info['type']!='gif' && function_exists('imagecreatetruecolor')?imagecreatetruecolor($thumb_width,$thumb_height):imagecreate($thumb_width,$thumb_height);
  imagecopyresampled($thumb_im,$im,0,0,0,0,$thumb_width,$thumb_height,$info['width'],$info['height']);
  if($info['type']=='gif' || $info['type']=='png')
  {
   $bgcolor=imagecolorallocate($thumb_im,0,255,0);
   imagecolortransparent($thumb_im,$bgcolor);
  }
  $imagefunc='image'.($info['type']=='jpg'?'jpeg':$info['type']);
  $thumbname='thumb_'.$info['name'].'.'.$info['type'];
  $imagefunc($thumb_im,$info['path'].$thumbname);
  imagedestroy($im);
  imagedestroy($thumb_im);
  return $info['path'].$thumbname;  
 }

 function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000')
 {
  $imageinfo=$this->info($image);
  $source_w=$imageinfo['width'];
  $source_h=$imageinfo['height'];
  $imagecreatefunc='imagecreatefrom'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  $im=$imagecreatefunc($image);
  if(!empty($watermarkimg) && file_exists($watermarkimg)) //添加图片水印
  {
   $iswaterimage=true;
   $watermarkinfo=$this->info($watermarkimg);
   $width=$watermarkinfo['width'];
   $height=$watermarkinfo['height'];
   $watermarkcreatefunc='imagecreatefrom'.($watermarkinfo['type']=='jpg'?'jpeg':$watermarkinfo['type']);
   $watermark_im=$watermarkcreatefunc($watermarkimg);
  }
  else //添加文字水印
  {
   $iswaterimage=false;
   if(!empty($w_color) && strlen($w_color)==7)
   {
    $r=hexdec(substr($w_color,1,2));
    $g=hexdec(substr($w_color,3,2));
    $b=hexdec(substr($w_color,5,2));
   }
   $temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/alger.ttf', $text);
   $width = $temp[2] - $temp[6];
   $height = $temp[3] - $temp[7];
   unset($temp);
  }
  switch($pos)
  {
   case 0:
    $wx = mt_rand(0,($source_w - $width));
    $wy = mt_rand(0,($source_h - $height));
    break;
   case 1:
    $wx = 5;
    $wy = 5;
    break;
   case 2:
    $wx = ($source_w - $width) / 2;
    $wy = 5;
    break;
   case 3:
    $wx = $source_w - $width-5;
    $wy = 5;
    break;
   case 4:
    $wx = 5;
    $wy = ($source_h - $height) / 2;
    break;
   case 5:
    $wx = ($source_w - $width) / 2;
    $wy = ($source_h - $height) / 2;
    break;
   case 6:
    $wx = $source_w - $width-5;
    $wy = ($source_h - $height) / 2;
    break;
   case 7:
    $wx = 5;
    $wy = $source_h - $height-5;
    break;
   case 8:
    $wx = ($source_w - $width) / 2;
    $wy = $source_h - $height-5;
    break;
   default:
    $wx = $source_w - $width-5;
    $wy = $source_h - $height-5;
    break;
  }
  if($iswaterimage)
  {
   if($imageinfo['type'] == 'png') {
    imagecopy($im, $watermark_im, $wx, $wy, 0, 0, $width, $height);
   } else {
    imagecopymerge($im, $watermark_im, $wx, $wy, 0, 0, $width, $height, $pct);
   }
  }
  else
  {
   imagestring($im,$w_font,$wx,$wy,$text,imagecolorallocate($im,$r,$g,$b));
  }
  $imagefunc='image'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  $imagefunc($im,$image);
  imagedestroy($im);
  return true;
 }

 function info($image)
 {
  $info=array();
  $info['size']=filesize($image);
  $imageinfo=getimagesize($image);
  $info['width']=$imageinfo[0];
  $info['height']=$imageinfo[1];
  $info['width_height']=$imageinfo[3];
  $info['mime']=$imageinfo['mime'];
  unset($imageinfo);
  $imageinfo=pathinfo($image);
  $info['path']=$imageinfo['dirname'].'/';
  $info['type']=strtolower($imageinfo['extension']); //图片类型,不含'.'
  $info['name']=$imageinfo['filename'];
  unset($imageinfo,$name);
  $this->info=$info;
  return $info;
 }
}

 

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.