`
bardo
  • 浏览: 372811 次
  • 性别: Icon_minigender_1
  • 来自: 上海
博客专栏
D1407912-ab64-3e76-ae37-b31aa4afa398
浅述PHP设计模式
浏览量:11641
9d6df9f7-91da-3787-a37c-0e826525dd5d
Zend Framewor...
浏览量:9994
85b628bd-a2ed-3de2-a4b1-0d34985ae8b6
PHP的IDE(集成开发环...
浏览量:9353
社区版块
存档分类
最新评论

PHP图象处理的函数imagecreatefromstring的问题

    博客分类:
  • PHP
阅读更多
PHP图象处理的函数imagecreatefromstring,可以直接将二进制图像字串(数据流)创建成图像资源。

因而,使用此函数免去了大量的文件IO。

比如,我们可以在必要时用imagegd2或imagegif等函数输出。但我们可以控制其输出到我们所要的变量中。如同以下代码:
 function _image_to_buffer($resource){ 
  ob_start();
  imagegif($resource);
  $output_buf= ob_get_contents();
  ob_end_clean(); 
  return $output_buf;
 } 


然后,再用imagecreatefromstring去做进一步的操作。也许你会说,这可能没有这样的必要。但是,对于GIF动画的处理,比如,完成功能如:resize animatedt gif,water mark  animatedt gif等,就有可能必须要这样做。

但很奇怪的是:imagecreatefromstring,当图象小于GIF的实际逻辑屏幕定义,并且 image left possition和image top possition不为0时,imagecreatefromstring会将此二者改为0,同时,图象不再是实际逻辑屏幕定义长与宽,而是image descroptor中的长与宽。不太清楚,这是否属于PHP图像处理函数的一个BUG。

并且,很让人遗憾的是 imagemagik 针对GIF动画制作缩略图时,也有这样的问题。 这样,原来的动画就出问题了。本来该在下面动的物体跑到了左上角。

因此,对于imagecreatefromstring,必须要另想办法,来改变这一个问题。
  $data = $this->get_frame_data($i); 
  $tmp_im = imagecreatefromstring ($data);
  $w= imagesx($tmp_im);
  $h= imagesy($tmp_im);
  $left_pos = $this->_word_to_int($this->gif_frames[$i]['img_dsc']['img_left_pos']);
  $top_pos = $this->_word_to_int($this->gif_frames[$i]['img_dsc']['img_top_pos']);
  $img_w = $this->_word_to_int($this->gif_frames[$i]['img_dsc']['img_w']);
  $img_h = $this->_word_to_int($this->gif_frames[$i]['img_dsc']['img_h']);

  if (($w< $this->gif_lsd['ls_w'])||($h<$this->gif_lsd['ls_h'])){
   $fra_im=imagecreatetruecolor($this->gif_lsd['ls_w'],$this->gif_lsd['ls_h']);
   if ($this->gif_frames[$i]['grph_ctrl_ext']['transparent']==1){    
    $color = $this->gif_gct['data'][$this->gif_frames[$i]['grph_ctrl_ext']['transparent_clr_idx']];
    imagepalettecopy($fra_im,$tmp_im);
    imagecolortransparent($fra_im,$color); 
    imagefilledrectangle($fra_im,0,0,$this->gif_lsd['ls_w'],$this->gif_lsd['ls_h'],$color);
   }
   imagecopy ($fra_im, $tmp_im, $left_pos, $top_pos, 0, 0, $img_w, $img_h);
  }   
  else 
   $fra_im=$tmp_im; 


这样,再用 $fra_im就正确了。
分享到:
评论

相关推荐

    PHP载入图像imagecreatefrom_gif_jpeg_png系列函数用法分析

    gif_jpeg_png系列函数用法,结合实例形式分析了php的图像载入函数imagecreatefromgif、imagecreatefromjpeg、imagecreatefrompng、imagecreatefromwbmp及imagecreatefromstring使用技巧,需要的朋友可以参考下

    PHP100视频教程 (三十五、PHP5文字图片混合水印与缩略图)

    1、介绍PHP水印原理和流程 2、ImageCreateFrom* 图片载入函数 imagecreatefromgif imagecreatefromjpeg ... imagecreatefromstring 3、imagecopy 图片合并函数 4、ImageCopyResized图片剪切函数

    PHP100视频教程 35:PHP5文字图片混合水印与缩略图

    1、介绍PHP水印原理和流程2、ImageCreateFrom* 图片载入函数 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng imagecreatefromwbmp imagecreatefromstring3、imagecopy 图片合并函数4、...

    php-blurhash:Blurhash 的纯 PHP 实现(https

    API 是稳定的,但任一方向的散列函数可能都不稳定。 Blurhash 是由为编写的算法,可将图像编码为短(~20-30 字节)ASCII 字符串。 当您将字符串解码回图像时,您将获得代表原始图像的渐变颜色。 这对于在加载前需要...

    PHP100视频教程 35:PHP5文字图片混合水印与缩略图.rar

    软件介绍 1、介绍PHP水印原理和流程 2、ImageCreateFrom* 图片载入函数  imagecreatefromgif  imagecreatefromjpeg ... imagecreatefromstring 3、imagecopy 图片合并函数 4、ImageCopyResized图片剪切函数

    PHP使用GIFEncoder类处理gif图片实例

    下面贴处理的源代码: 复制代码 代码如下: &lt;?php require_once(“gifencoder.php”); //载入编码 文件 $gif = new GIFEncoder(); //实例化gif解码对象 $gif-&gt;load(“test.gif”); //载入要解码的gif图像 for($...

    php给图片加文字水印

    /*imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符,其表达了从给定字符串得来的图像 图像格式将自动监测,只要php支持jpeg,png,gif,wbmp,gd2.*/ $font = './t1.ttf'; $bl

    Class_QRCode.php

    $QR = imagecreatefromstring(file_get_contents&#40;$QRUrl&#41;); $logo = imagecreatefromstring(file_get_contents&#40;$logo&#41;); $QR_width = imagesx($QR); //二维码图片宽度 $QR_height = imagesy($QR...

    PHP 中 Orientation 属性判断上传图片是否需要旋转

    当使用苹果的iOS系统拍照上传图片的时候,可能会遇到图片被旋转的问题,这主要是取决于你拍照时拍照按钮的位置。假设拍照时你把手机旋转过来底部朝上,那拍出来的照片也是被旋转了的。 下面的代码将确保所有上传的...

    php 图片上添加透明度渐变的效果

    ////$imgsrc = imagecreatefromstring($strimgsrc); $imgsrc = imagecreatefromjpeg(“5307754.jpg”); $imgsrcw = imagesx($imgsrc); $imgsrch = imagesy($imgsrc); $width = 30; $x1 = 2; $x2 = $imgsrcw – $x1 ...

    PHP实现原比例生成缩略图的方法

    $im = imagecreatefromstring($imgstream); $x = imagesx($im);//获取图片的宽 $y = imagesy($im);//获取图片的高 // 缩略后的大小 $xx = 140; $yy = 200; if($x&gt;$y){ //图片宽大于高 $sx = abs(($y-$x)/2); $sy =...

    php利用gd库为图片添加水印

    $dst = imagecreatefromstring(file_get_contents&#40;$dst_path&#41;); $src = imagecreatefromstring(file_get_contents&#40;$src_path&#41;); //获取水印图片的宽高 list($src_w, $src_h) = getimagesize($src_...

    php文字水印和php图片水印实现代码(二种加水印方法)

    //创建图片的实例$dst = imagecreatefromstring(file_get_contents&#40;$dst_path&#41;); //打上文字$font = ‘./simsun.ttc’;//字体$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色imagefttext($...

    php下图片文字混合水印与缩略图实现代码

    一 imageCreateFrom* 图片载入函数 //针对不同的后缀名图片 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng imagecreatefromwbmp imagecreatefromstring 使用格式:imagecreatefromgif(“jjj.gif”);...

    使用gd库实现php服务端图片裁剪和生成缩略图功能分享

    //创建源图的实例$src = imagecreatefromstring(file_get_contents&#40;$src_path&#41;);//裁剪开区域左上角的点的坐标$x = 100;$y = 12;//裁剪区域的宽和高$width = 200;$height = 200;//最终保存成图片的宽和高,...

Global site tag (gtag.js) - Google Analytics