php 如何将GIF动态图像缩放成指定大小后依旧保持动态?

作者&投稿:表林 (若有异议请与网页底部的电邮联系)
你好,php上传gif图片,怎么缩放大小啊,我的缩放后,小的gif图像就不动了~

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>

上传图片后,可以对原图片利有上边的代码处理。缩放

GD库操作即可

此js代码来自百度logo的处理,还是很好用的,需要注意的地方就是proMaxHeight,proMaxWidth 参数的动态获取,你可以直接用php赋值,或者用smarty phplib之类的模板,这个不难

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>test</title>

<script language=Javascript>

var proMaxHeight = 50;

var proMaxWidth = 50;

function proDownImage(ImgD){

   var image=new Image();

   image.src=ImgD.src;

   if(image.width>0 && image.height>0){

   var rate = (proMaxWidth/image.width < proMaxHeight/image.height)?proMaxWidth/image.width:proMaxHeight/image.height;

  if(rate <= 1){ 

   ImgD.width = image.width*rate;

   ImgD.height =image.height*rate;

  }

  else {

             ImgD.width = image.width;

             ImgD.height =image.height;

         }

   }

}

//-->

</script>

</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0" scroll="no">

<img src="./pp.gif" onload=proDownImage(this); />

</body>

</html>



php的GD库是不支持GIF格式的动态图片重写的,就直接限制图片<img />标签的大小算了

美图秀秀