1. 添加水印

经测试,效果不错,jpg 尤佳:

<?php
define('WATERMARK_OVERLAY_IMAGE', $_SERVER['DOCUMENT_ROOT'] .'/public/assets/logo.png');/*
* $img 是图片的绝对路径
* 用法:watermark($_SERVER['DOCUMENT_ROOT'] .'/public/images/bg.jpg');
*/
function watermark($img)
{ob_start();// Set content typeheader('Content-type: image/jpg');// Cerate watermark$wm = imagecreatefrompng(WATERMARK_OVERLAY_IMAGE);// getting dimensions of watermark image$wm_size = getimagesize(WATERMARK_OVERLAY_IMAGE);// Create image$image = imagecreatefromjpeg($img);// Get image size$size = getimagesize($img);// placing the watermark center$dest_x = ($size[0] - $wm_size[0])/2;  $dest_y = ($size[1] - $wm_size[1])/2;// blending the images togetherimagealphablending($image, true);imagealphablending($wm, true); // creating the new imageimagecopy($image, $wm, $dest_x, $dest_y, 0, 0, $wm_size[0] , $wm_size[1]);// Send image to the browser imagejpeg($image, null, 100);//Save image in cache// destroy image and watermakimagedestroy($image);imagedestroy($wm);$output = ob_get_contents();ob_end_clean();unlink($img);$size = strlen($output);     $ftp  = @fopen($img, "a");    fwrite($ftp,$output);     fclose($ftp);
}

 2. png2jpg

You need to create a fresh image with a white (or whatever you want) background and copy the none-transparent pixels from the png to that image:

function png2jpg($originalFile, $outputFile, $quality)
{$source = imagecreatefrompng($originalFile);$image = imagecreatetruecolor(imagesx($source), imagesy($source));$white = imagecolorallocate($image, 255, 255, 255);imagefill($image, 0, 0, $white);imagecopy($image, $source, 0, 0, 0, 0, imagesx($image), imagesy($image));imagejpeg($image, $outputFile, $quality);imagedestroy($image);imagedestroy($source);
}

3. gif2jpg

For those interested in my solution, I simply used the built in GD PHP functions. I have little experience dealing with gif files so I was expecting this to be difficult. The fact of the matter is the CodeIgniter Image_lib and extended library (Which I never got to work properly) is overkill for this.

$image = imagecreatefromgif($path_to_gif_image);
imagejpeg($image, $output_path_with_jpg_extension);

4. Resizing images with PHP

The following script will easily allow you to resize images using PHP and the GD library. If you’re looking to resize uploaded images or easily generate thumbnails give it a try

Update: Looking to resize transparent PNG’s and GIF’s? We’ve updated our original code, take a look at http://www.white-hat-web-design.co.uk/blog/retaining-transparency-with-php-image-resizing/

Usage

Save the code from the ‘the code’ section below as SimpleImage.php and take a look at the following examples of how to use the script.

The first example below will load a file named picture.jpg resize it to 250 pixels wide and 400 pixels high and resave it as picture2.jpg

<?phpinclude('SimpleImage.php');$image = new SimpleImage();$image->load('picture.jpg');$image->resize(250,400);$image->save('picture2.jpg');
?>
If you want to resize to a specifed width but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function.
<?phpinclude('SimpleImage.php');$image = new SimpleImage();$image->load('picture.jpg');$image->resizeToWidth(250);$image->save('picture2.jpg');
?>

You may wish to scale an image to a specified percentage like the following which will resize the image to 50% of its original width and height
<?phpinclude('SimpleImage.php');$image = new SimpleImage();$image->load('picture.jpg');$image->scale(50);$image->save('picture2.jpg');
?>

You can of course do more than one thing at once. The following example will create two new images with heights of 200 pixels and 500 pixels

<?phpinclude('SimpleImage.php');$image = new SimpleImage();$image->load('picture.jpg');$image->resizeToHeight(500);$image->save('picture2.jpg');$image->resizeToHeight(200);$image->save('picture3.jpg');
?>

The output function lets you output the image straight to the browser without having to save the file. Its useful for on the fly thumbnail generation

<?phpheader('Content-Type: image/jpeg');include('SimpleImage.php');$image = new SimpleImage();$image->load('picture.jpg');$image->resizeToWidth(150);$image->output();
?>

The following example will resize and save an image which has been uploaded via a form

<?phpif( isset($_POST['submit']) ) {include('SimpleImage.php');$image = new SimpleImage();$image->load($_FILES['uploaded_image']['tmp_name']);$image->resizeToWidth(150);$image->output();} else {?><form action="upload.php" method="post" enctype="multipart/form-data"><input type="file" name="uploaded_image" /><input type="submit" name="submit" value="Upload" /></form><?php}
?>

The code

<?php/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/class SimpleImage {var $image;var $image_type;function load($filename) {$image_info = getimagesize($filename);$this->image_type = $image_info[2];if( $this->image_type == IMAGETYPE_JPEG ) {$this->image = imagecreatefromjpeg($filename);} elseif( $this->image_type == IMAGETYPE_GIF ) {$this->image = imagecreatefromgif($filename);} elseif( $this->image_type == IMAGETYPE_PNG ) {$this->image = imagecreatefrompng($filename);}}function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {if( $image_type == IMAGETYPE_JPEG ) {imagejpeg($this->image,$filename,$compression);} elseif( $image_type == IMAGETYPE_GIF ) {imagegif($this->image,$filename);} elseif( $image_type == IMAGETYPE_PNG ) {imagepng($this->image,$filename);}if( $permissions != null) {chmod($filename,$permissions);}}function output($image_type=IMAGETYPE_JPEG) {if( $image_type == IMAGETYPE_JPEG ) {imagejpeg($this->image);} elseif( $image_type == IMAGETYPE_GIF ) {imagegif($this->image);} elseif( $image_type == IMAGETYPE_PNG ) {imagepng($this->image);}}function getWidth() {return imagesx($this->image);}function getHeight() {return imagesy($this->image);}function resizeToHeight($height) {$ratio = $height / $this->getHeight();$width = $this->getWidth() * $ratio;$this->resize($width,$height);}function resizeToWidth($width) {$ratio = $width / $this->getWidth();$height = $this->getheight() * $ratio;$this->resize($width,$height);}function scale($scale) {$width = $this->getWidth() * $scale/100;$height = $this->getheight() * $scale/100;$this->resize($width,$height);}function resize($width,$height) {$new_image = imagecreatetruecolor($width, $height);imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());$this->image = $new_image;}      }
?>

Further Information

This script will eventually be developed further to include functions for easily sharpening, bluring, cropping, brightening and colouring images.

from:  http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

php 添加水印, 格式转换, 变换大小 Watermark, png2jpg, resize相关推荐

  1. mac电脑如何转换图片格式及修改大小?

    mac电脑如何转换图片格式及修改大小? !转换图片格式 !转换图片格式 !转换图片大小 以上,希望能对大家有所帮助!

  2. 图片格式转换大小调整工具_如何轻松快速地将图片转换到JPG/JPEG/PNG/BMP/TIFF

    万兴优转可用作为图片转换器,帮助您批量更改图片格式,且不会丢失任何质量.例如,您可以将PNG转换到JPG或其他格式,反之亦然.您还可以通过更改图片宽度和高度来调整图片大小,或者通过裁剪,旋转,添加效果 ...

  3. R pdf大小_全能格式转换工具分享,PDF 转 Word、视频图片格式转换等

    前言 经常有人问起 PDF 转 Word.视频格式转换方面的问题.如果日常需要和文档.视频打交道,那么格式转换也是经常会有的需求了. 下面分享几款「万能格式转换工具」,无论是视频.图片.音频,还是文档 ...

  4. 最全可白嫖之高光谱图像数据处理(格式转换,数据增强,通道剪切,大小裁剪,光谱显示,折线图表示)

    目录 (一)高光谱谱格式转换之rar转mat格式 ①RAW转tiff步骤: ②tiff转mat步骤: (二)两种方法把高光谱图像缩放到0-1的数据集 (三)高光谱数据预处理成规定大小和规格的数据集 ( ...

  5. html在电脑上转换字体怎么变了,电脑网页字体怎么变换大小

    电脑网页字体变大有时候会让人产生不舒服的感觉,有时候就想网页字体变换一下大小,下面是学习啦小编整理的电脑网页字体变换大小的方法,供您参考. 电脑网页字体变换大小的方法一 首先,建议如果是私人电脑的情况 ...

  6. PHP做视频网站,让程序自动实现视频格式转换、设置视频大小、生成视频缩略图...

    一.PHP实现转换 在做视频网站的时候,最头痛的问题可能是格式转换.视频缩略图等.下面我将用PHP实现这一些功能.PHP是没有自带视频的函数,所以会用到第三方的软件工具来实现. 二.什么是FFmpeg ...

  7. 文档大小超出上传限制怎么办_又一神器!免费万能格式转换:视频、音频、文档、图片等随意转换...

    一.神器介绍 我们在办公.学习.工作过程中总是会碰到需要将PDF文件转换成可编辑的 Word Doc.DocX. Excel XIs. XIsx. PowerPoint PPT. PPTX 等格式.今 ...

  8. drawboard pdf拆分文件_PDF处理神器,几秒钟搞定格式转换+压缩+加水印+解密!

    PDF对于一个科研学习/工作者来说几乎每天都会接触,尤其是PDF格式转换的时候不知道怎么办,还有些文件加密了只能看不能编辑,有些几十页甚至几百页的文件每次翻看起来都特别麻烦,想防盗给自己的pdf文件加 ...

  9. 对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作

    对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作 1 /** 2 * <html> 3 * <body> 4 * <P> Copyright 1994 Jso ...

最新文章

  1. go语言json解析的坑 注意事项
  2. IOS开发基础之画板案例软件的开发
  3. java 创建数组工具类_用Java创建数组工具类ArrayTool
  4. 精彩回顾丨2021数据库大咖讲坛(第7期)视频PPT互动问答
  5. php求平均值的函数_剔除两侧极值求平均Excel公式 去掉最大值最小值
  6. 利用Simple-RTMP-Server(SRS)来进行直播
  7. 万字长文!Go 后台项目架构思考与重构
  8. Vue 学习随笔四 - 路由介绍
  9. 怎样用计算机做周计划表,电脑如何制作学生学习计划表
  10. 用CAD看图软件查找文字需要怎么做
  11. 吉时利DMM6500数字万用表,更高的生产测试量和更低的测试成本
  12. Zynga公布2020年第三季度财务业绩
  13. 减一技术实现求a的n次幂
  14. 《machine learning in action》机器学习 算法学习笔记 决策树模型
  15. python-图片颜色转换-将红绿两色互换
  16. Codeforces 85D Sum of Medians[线段树]
  17. 连接型智能BPM引擎——雀书
  18. 将Element UI的时间选择器(DatePicker)的Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)转化为XXXX-XX-XX的格式
  19. 2019-2-23小图三张
  20. abb机器人指令手册_ABB机器人图形化编程wizard

热门文章

  1. 信息学奥赛一本通C++语言——1027:输出浮点数
  2. 29 FI配置-财务会计-外币评估-分配到评估范围和会计核算原则
  3. simpledateformat线程不安全_ArrayList为什么线程不安全?
  4. CSS3-新增属性选择器
  5. HTML5新特性基础学习笔记下
  6. Vue框架里使用Swiper - 安装篇
  7. uart怎么判断帧错误_UART通讯总线工作原理的理解--龚玉山
  8. python 当前文件路径获取方式_python中获取文件路径的几种方式
  9. 创意夜晚行驶迷路网站404页面源码
  10. 计算机应用入学考试,本科【计算机应用】入学考试模拟试题.doc