多功能的php图片处理类,php图形图像处理技术

本文目录一览:

我这有个已经实现了php上传图片的功能的类,但是现在还要给上传的每张图片加水印,请高手帮忙……

给你一个封装的图片处理的类吧!包含缩放和剪切功能!

构造方法只需要传递图片所在路径即可!对应方法及参数都有注释!

请给予最佳答案!!

?php

class Img{

private $path;

//构造方法,初始化图片信息

function __construct($path=’./imgs/’){

$this-path=rtrim($path,’/’).’/’;

}

/**

* 对图片进行缩放

* 参数对应:文件名 缩放后宽度 缩放后高度 缩放后图片名前缀

*/

function thumb($name,$width,$height,$pre=”th_”){

if(file_exists($this-path.$name)){

$imgInfo=$this-getInfo($name);

$img=$this-getImg($name,$imgInfo);

$newSize=$this-getNewSize($name,$width,$height,$imgInfo);

$newImg=$this-getNewInfo($img,$newSize,$imgInfo);

return $this-createNewImage($newImg, $pre.$name, $imgInfo);

}else{

echo ‘图片’.$this-path.$name.’不存在,请检查文件名及路径是否填写正确’;

}

}

//辅助图片处理,获取图片的宽、高、类型属性

private function getInfo($name){

$temp=getImageSize($this-path.$name);

$imgInfo[‘width’]=$temp[0];

$imgInfo[‘height’]=$temp[1];

$imgInfo[‘type’]=$temp[2];

return $imgInfo;

}

//辅助图片处理,获取创建的图片资源

private function getImg($name,$imgInfo){

$src=$this-path.$name;

switch($imgInfo[‘type’]){

case 1:

$img=imagecreatefromgif($src);

break;

case 2:

$img=imagecreatefromjpeg($src);

break;

case 3:

$img=imagecreatefrompng($src);

break;

}

return $img;

}

//辅助图片处理,获取创建的图片资源

private function getNewSize($name,$width,$height,$imgInfo){

$newSize[‘width’]=$imgInfo[‘width’];

$newSize[‘height’]=$imgInfo[‘height’];

if($width$imgInfo[‘width’]){

$newSize[‘width’]=$width;

}

if($height$imgInfo[‘height’]){

$newSize[‘height’]=$height;

}

if($imgInfo[“width”]*$newSize[“width”] $imgInfo[“height”] * $newSize[“height”]){

$newSize[“height”]=round($imgInfo[“height”]*$newSize[“width”]/$imgInfo[“width”]);

}else{

$newSize[“width”]=round($imgInfo[“width”]*$newSize[“height”]/$imgInfo[“height”]);

}

print_r($newSize);

return $newSize;

}

//辅助图片处理,获取缩放的图片资源

private function getNewInfo($img,$newSize,$imgInfo){

$newImg=imagecreatetruecolor($newSize[‘height’],$newSize[‘height’]);

$otsc=imagecolortransparent($img);

if($otsc =0 $otsc = imagecolorstotal($img)){

$tran=imagecolorsforindex($img, $otsc);

$newt=imagecolorallocate($newImg, $tran[“red”], $tran[“green”], $tran[“blue”]);

imagefill($newImg, 0, 0, $newt);

imagecolortransparent($newImg, $newt);

}

imagecopyresized($newImg, $img, 0, 0, 0, 0, $newSize[“width”], $newSize[“height”], $imgInfo[“width”], $imgInfo[“height”]);

imagedestroy($img);

return $newImg;

}

//辅助图片处理,创建新的图片

private function createNewImage($newImg, $newName, $imgInfo){

switch($imgInfo[“type”]){

case 1://gif

$result=imageGif($newImg, $this-path.$newName);

break;

case 2://jpg

$result=imageJPEG($newImg, $this-path.$newName);

break;

case 3://png

$return=imagepng($newImg, $this-path.$newName);

break;

}

imagedestroy($newImg);

return $newName;

}

/**

* 对图片加水印

* 参数对应:需水印图片 水印图片 加水印后图片名前缀

*/

function waterMark($name,$wname,$pre=”wa_”){

if(file_exists($this-path.$name)){

if(file_exists($this-path.$wname)){

$info=$this-getInfo($name);

$winfo=$this-getInfo($wname);

if($p=$this-getPosition($info,$winfo)){

$img=$this-getImg($name,$info);

$wImg=$this-getImg($wname,$winfo);

imagecopy($img, $wImg, $p[“x”], $p[“y”], 0, 0, $winfo[“width”], $winfo[“height”]);

imagedestroy($wImg);

return $this-createNewImage($img,$pre.$name,$info);

}else{

echo ‘水印图片尺寸大于原图片尺寸’;

}

}else{

echo ‘水印图片’.$this-path.$wname.’不存在,请检查文件名及路径是否填写正确’;

}

}else{

echo ‘图片’.$this-path.$name.’不存在,请检查文件名及路径是否填写正确’;

}

}

//辅助图片处理,获取水印图片应处坐标

private function getPosition($info,$winfo){

if($info[‘width’]$winfo[‘width’]||$info[‘height’]$winfo[‘height’]){

return false;

}

$x=$info[‘width’]-$winfo[‘width’];

$y=$info[‘height’]-$winfo[‘height’];

return array(‘x’=$x,’y’=$y);

}

/**

* 图片剪切函数

* 对应参数:原图片 X坐标 Y坐标 宽度 高度

*/

function cut($name,$x,$y,$width,$height,$pre=’cx_’){

$imgInfo=$this-getInfo($name);

$img=$this-getImg($name,$imgInfo);

$newImg=imagecreatetruecolor($width,$height);

imagecopyresampled($newImg,$img,0,0,$x,$y,$width,$height,$width,$height);

return $this-createNewImage($newImg, $pre.$name, $imgInfo);

}

}

求PHP图片处理类。

裁剪图片。合并,可以通过相应的函数进行,先确定图片的后缀,再确定使用的函数,具体可查看手册,getimagesize,imagecreatefromgif,imagecreatefromjpeg,imagecopy

PHP图像处理函数有哪些

php图像处理函数大全

php图片处理代码分享,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等。需要的朋友可以参考下

一、创建图片资源

imagecreatetruecolor(width,height);

imagecreatefromgif(图片名称);

imagecreatefrompng(图片名称);

imagecreatefromjpeg(图片名称);画出各种图像

imagegif(图片资源,保存路径);

imagepng()

imagejpeg();

二、获取图片属性

imagesx(res//宽度

imagesy(res//高度

getimagesize(文件路径)

返回一个具有四个单元的数组。索引

0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 =

PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10

= JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的

IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height=”yyy” width=”xxx””,可直接用于 IMG

标记。

销毁图像资源

imagedestroy(图片资源);

三、透明处理

PNG、jpeg透明色都正常,只有gif不正常

imagecolortransparent(resource

image [,int

color])//将某个颜色设置成透明色

imagecolorstotal()

imagecolorforindex();

四、图片的裁剪

imagecopyresized()

imagecopyresampled();

五、加水印(文字、图片)

字符串编码转换string iconv ( string $in_charset ,

string $out_charset , string $str )

六、图片旋转

imagerotate();//制定角度的图片翻转

七、图片的翻转

沿X轴 沿Y轴翻转

八、锐化

imagecolorsforindex()

imagecolorat()

PHP 图片处理

图片路径一定要基于当前php运行所在的路径去写,./图片 是当前目录,../图片 是上级目录,注意规范

php图片处理类怎么用

Grafika是一个PHP图像处理库,是基于Imagick和GD,可以用于改变图片大小,剪裁,比较,添加水印等等功能。还有感知哈希,高级图像过滤,绘制贝塞尔曲线等功能,可谓非常强大。

php 怎么上传完图片之后,给这个图片打水印,并且保存在另一个文件夹

这个php中的图片处理类完全足够了,使用图片水印

$groundImg = “DSC05940.jpeg”;

$groundInfo = getimagesize($groundImg);

$ground_w = $groundInfo[0];

//print_r($groundInfo);

$ground_h = $groundInfo[1];

switch($groundInfo[2]){

case 1:

$ground_im = imagecreatefromgif($groundImg);

break;

case 2:

$ground_im = imagecreatefromjpeg($groundImg);

break;

case 3:

$ground_im = imagecreatefrompng($groundImg);

break;

}

$waterImg = “DSC05949.jpeg”;

$imgInfo =getimagesize($waterImg);

$water_w = $imgInfo[0];

$water_w = $imgInfo[1];

switch($imgInfo[2]){

case 1:

$water_im = imagecreatefromgif($waterImg);

break;

case 2:

$water_im = imagecreatefromjpeg($waterImg);

break;

case 3:

$water_im = imagecreatefrompng($waterImg);

break;

}

imagecopy($ground_im,$water_im,100,100,0,0,500,500);

header(“Content-type: image/jpeg”);

imagejpeg($ground_im);

这些都很麻烦,建议使用框架,很多框架都提供了图片处理类供使用

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/187097.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-11-27 13:35
下一篇 2024-11-27 13:35

相关推荐

  • Zlios——一个多功能的开发框架

    你是否在开发过程中常常遇到同样的问题,需要不断去寻找解决方案?你是否想要一个多功能、易于使用的开发框架来解决这些问题?那么,Zlios就是你需要的框架。 一、简介 Zlios是一个…

    编程 2025-04-29
  • 从不同位置观察同一个物体,看到的图形一定不同

    无论是在平时的生活中,还是在科学研究中,都会涉及到观察物体的问题。而我们不仅要观察物体本身,还需要考虑观察的位置对观察结果的影响。从不同位置观察同一个物体,看到的图形一定不同。接下…

    编程 2025-04-28
  • 使用boofcv进行图像处理和机器视觉

    本文将详细介绍使用boofcv进行图像处理和机器视觉的方法和实践。首先,我们将介绍boofcv的概述和安装方法,然后分别介绍它的图像处理、相机校准和机器学习功能。 一、概述和安装 …

    编程 2025-04-28
  • Python设置图形填充颜色为绿色的语句

    图形设计是计算机科学中一个重要的分支,而Python语言也是最受欢迎的图形设计语言之一。Python凭借其易用性和开源特性,赢得了很多开发者和程序员的青睐。本文将围绕如何设置Pyt…

    编程 2025-04-27
  • Python图片处理

    Python是一种高级编程语言,具有快速开发应用程序的能力。Python有大量的库和工具可以处理不同类型的图像。Python在图像识别、处理和分析方面是一种非常有用的语言。 一、读…

    编程 2025-04-27
  • Python中的delattr:一个多功能的属性删除方法

    在Python编程中,delattr()是一个十分强大常用的函数,可以方便的删除一个对象的属性,并且使用起来非常灵活。接下来将从多个方面详细阐述Python中的delattr()方…

    编程 2025-04-27
  • 故障树中未探明事件的图形符号

    故障树是一种可视化的分析工具,用于确定系统或过程中故障的原因和可能的根源。故障树中未探明事件的图形符号是指在分析中无法找到前驱事件的事件,本文将从多个方面对其进行详细阐述。 一、符…

    编程 2025-04-27
  • Matlab局部放大——图像处理的神器

    一、什么是Matlab局部放大? Matlab是一个高级技术计算语言和交互式环境,常被用来进行科学计算和工程设计等领域的计算和可视化操作。局部放大指对一张图像或视频中感兴趣的区域进…

    编程 2025-04-25
  • kfloatwin.dll——多功能窗口驱动程序

    一、介绍 kfloatwin.dll是一个轻量级多功能窗口驱动程序,具有窗口置顶、窗口拖动、窗口透明等功能。本文将从功能、使用、实现等多个方面进行详细阐述。 二、功能 1、窗口置顶…

    编程 2025-04-24
  • log4cpp:多功能的C++日志库

    一、简介 log4cpp是一个支持多线程的C++日志库,能够让程序员在应用程序中方便地记录日志输出,分级管理日志信息,并灵活地控制日志记录方式。 log4cpp的设计目标是提供一种…

    编程 2025-04-24

发表回复

登录后才能评论