1. <?php
  2. include("SimpleImage.php");//图片处理类在下面
  3.  
  4. $url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";
  5. $picfile = down($url);//下载图片(下载图片的路径可以处理完成后清空,这里未进行处理)
  6. $res = new SimpleImage();//图片处理实例
  7. $res = $res->load($picfile);
  8. $tmpfile = tempfile().'.jpg';//创建一个路径文件用来保存图片
  9. $width = '30';//设定图片的宽度
  10. $res->resizeToWidth($width);
  11. $res->save($tmpfile);//把处理后的图片保存(无.jpg后缀)
  12. //这里总共产生了3个文件,一个是图片下载的文件,一个是临时文件,最后一个是处理的图片文件。需要优化清理掉前两个文件。
  13.  
  14. function down($url)
  15. {
  16. $http = array();
  17. $header = "http://f3.v.veimg.cn";
  18. if ($header) {
  19. $http['header'] = $header;
  20. }
  21.  
  22. $http['timeout'] = 50;
  23.  
  24. $ctx = stream_context_create(array(
  25. 'http' => $http,
  26. ));
  27. $content = @file_get_contents($url, 0, $ctx);
  28. sleep(1);
  29.  
  30. if (!$content) {
  31. return false;
  32. }
  33.  
  34. $tmpfile = tempfile();
  35.  
  36. file_put_contents($tmpfile, $content);
  37.  
  38. return $tmpfile;
  39. }
  40.  
  41. function tempfile()
  42. {
  43. $path = dirname(__FILE__);
  44. $path .= '/spider/' . date('Ymd') . '/'.date('His').'-' . (int)(time() / 300);
  45.  
  46. if (!file_exists($path)) {
  47. mkdir($path, 0777, true);
  48. }
  49.  
  50. do {
  51. $file = $path . '/' . dechex(mt_rand());
  52. }
  53. while (file_exists($file));
  54.  
  55. touch($file);
  56.  
  57. return $file;
  58. }

图片处理类:

  1. <?php
  2.  
  3. /*
  4. * File: SimpleImage.php
  5. * Author: Simon Jarvis
  6. * Copyright: 2006 Simon Jarvis
  7. * Date: 08/11/06
  8. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details:
  19. * http://www.gnu.org/licenses/gpl.html
  20. *
  21. */
  22.  
  23. class SimpleImage {
  24.  
  25. var $image;
  26. var $image_type;
  27.  
  28. function load($filename) {
  29.  
  30. $image_info = getimagesize($filename);
  31. $this->image_type = $image_info[2];
  32. if( $this->image_type == IMAGETYPE_JPEG ) {
  33.  
  34. $this->image = @imagecreatefromjpeg($filename);
  35. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  36.  
  37. $this->image = @imagecreatefromgif($filename);
  38. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  39.  
  40. $this->image = @imagecreatefrompng($filename);
  41. }
  42.  
  43. if (!$this->image) {
  44. return false;
  45. }
  46.  
  47. return $this;
  48. }
  49.  
  50. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  51.  
  52. if( $image_type == IMAGETYPE_JPEG ) {
  53. imagejpeg($this->image,$filename,$compression);
  54. } elseif( $image_type == IMAGETYPE_GIF ) {
  55.  
  56. imagegif($this->image,$filename);
  57. } elseif( $image_type == IMAGETYPE_PNG ) {
  58.  
  59. imagepng($this->image,$filename);
  60. }
  61. if( $permissions != null) {
  62.  
  63. chmod($filename,$permissions);
  64. }
  65. }
  66. function output($image_type=IMAGETYPE_JPEG) {
  67.  
  68. if( $image_type == IMAGETYPE_JPEG ) {
  69. imagejpeg($this->image);
  70. } elseif( $image_type == IMAGETYPE_GIF ) {
  71.  
  72. imagegif($this->image);
  73. } elseif( $image_type == IMAGETYPE_PNG ) {
  74.  
  75. imagepng($this->image);
  76. }
  77. }
  78. function getWidth() {
  79.  
  80. return imagesx($this->image);
  81. }
  82. function getHeight() {
  83.  
  84. return imagesy($this->image);
  85. }
  86. function resizeToHeight($height) {
  87.  
  88. $ratio = $height / $this->getHeight();
  89. $width = $this->getWidth() * $ratio;
  90. $this->resize($width,$height);
  91. }
  92.  
  93. function resizeToWidth($width) {
  94. if ($this->getWidth() < $width) {
  95. $width = $this->getWidth();
  96. }
  97. $ratio = $width / $this->getWidth();
  98. $height = $this->getheight() * $ratio;
  99. $this->resize($width,$height);
  100. }
  101.  
  102. function scale($scale) {
  103. $width = $this->getWidth() * $scale/100;
  104. $height = $this->getheight() * $scale/100;
  105. $this->resize($width,$height);
  106. }
  107.  
  108. function resize($width,$height) {
  109. $new_image = imagecreatetruecolor($width, $height);
  110. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  111. $this->image = $new_image;
  112. }
  113.  
  114. function resize2($width,$height) {
  115. $new_image = imagecreatetruecolor($width, $height);
  116. if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
  117. $current_transparent = imagecolortransparent($this->image);
  118. if($current_transparent != -1) {
  119. $transparent_color = imagecolorsforindex($this->image, $current_transparent);
  120. $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  121. imagefill($new_image, 0, 0, $current_transparent);
  122. imagecolortransparent($new_image, $current_transparent);
  123. } elseif( $this->image_type == IMAGETYPE_PNG) {
  124. imagealphablending($new_image, false);
  125. $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
  126. imagefill($new_image, 0, 0, $color);
  127. imagesavealpha($new_image, true);
  128. }
  129. }
  130. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  131. $this->image = $new_image;
  132. }
  133.  
  134. }

php处理图片实现的更多相关文章

  1. nodejs处理图片、CSS、JS链接

    接触Nodejs不深,看到页面上每一个链接都要写一个handler,像在页面显示图片,或者调用外部CSS.JS文件,每个链接都要写一个handler,觉得太麻烦,是否可以写个程序出来,能够自动识别图片 ...

  2. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  3. Filter Effects - 使用 CSS3 滤镜处理图片

    CSS3 Filter(滤镜)属性提供了提供模糊和改变元素颜色的功能.CSS3 Fitler 常用于调整图像的渲染.背景或边框显示效果.这里给大家分享的这个网站,大家可以体验下 CSS3 对图片的处理 ...

  4. 安装glue,用glue批量处理图片的步骤

     glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...

  5. delphi 处理图片(剪切,压缩)

    剪切bmp:效果为指定的rect大小,若图片比rect小,则会放大. 都要uses Vcl.Imaging.jpeg; 需要注意的是FMX里也需要jpeg的支持,虽然没引用编译器不会报错,但用到jpg ...

  6. 用Photoshop处理图片使背景透明

    用Photoshop处理图片使背景透明 打开一张图片 双击背景或者右键背景图层,新建一个图层, 选择魔棒工具,单击图片, 会自动选择颜色相近的范围 按下键盘的delete键,就可以删除魔棒所选择的区域 ...

  7. Python 将pdf转换成txt(不处理图片)

    上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将d ...

  8. qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题

    在用opencv处理图片显示在qt label上的时候遇到不是问题 1. qt上要用qimage形式才干显示 IplImage转成 Qimage 彩色图像转换 IplImage  *fram; QIm ...

  9. Eclipse中处理图片引包问题

    在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...

  10. photoshop动作面板批量处理图片边框技巧

    1,想给图片加上边框,在不改变图片大小的前提下,可以这样做:ctrl+a,全选图片,然后“编辑”-----“描边”,在跳出来的选项卡里面可以设置边框颜色,大小,位置,及混合模式, ,我们设置好了,就可 ...

随机推荐

  1. [AngularJS] 常用指令

    常用指令 ng-hide指令,用于控制部分HTML元素可见(ng-hide="false")和不可见状态(ng-hide="true"),如下: <div ...

  2. 串行移位锁存并行输出可级联器件74HC595

    一.背景 老同学今天突然咨询关于74HC595,自己没用过,同学说可以级联10级!10级?我艹,这么叼,级联又是 什么鬼,这勾起了我极大兴趣,二话不说,手册down下来研究,并在此做个记录. 二.正文 ...

  3. Swift3.0P1 语法指南——集合类型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  4. CMD代码页

    不同字符编码在CMD模式下会出现乱码,需要使用 chcp 代码页 命令来更改代码页显示正常. UTF-8  65001 简体中文 936 437          美国 850          多语 ...

  5. js字符串与16进制互相转换

    // \x65\x76\x61\x6c是否启用\x加密 <script type="text/javascript"> function JavaDe() { var ...

  6. LVS NAT模式

    LVS-NAT 三台虚拟机都是centos 6.5 关闭防火墙和selinux 角色 IP地址 备注 LVS负载调度器 eth0:192.168.119.128(内网) eth1:192.168.94 ...

  7. zabbix 3.0.4 Nginx 性能监控

    搭建Nginx 安装pcre-devel .zlib-devel支持包 [root@test /]# yum -y install pcre-devel zlib-devel 创建nginx用户 [r ...

  8. BZOJ2631——tree

    1.题目大意:bzoj1798的lct版本 2.分析:这个把线段树改成splay就好 #include <stack> #include <cstdio> #include & ...

  9. Python自动化之多进程

    多进程multiprocessing from multiprocessing import Process import os def info(title): print(title) print ...

  10. 越狱后的ios如何用apt-get 安装各种命令

    越狱后的ios如何用apt-get 安装各种命令   iphone越狱后想玩linux. 1. ssh 客户端:ssh Term Pro. 2. 只装客户端是连不上的,还得一个 ssh connect ...