很多站长朋友们都不太清楚php图片放大缩小,今天小编就来给大家整理php图片放大缩小,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php图片可以等比例的缩放吗 2、 php实现图片等比例缩放代码 3、 求php图片缩放处理函数 4、 php 怎么压缩图片的大小 5、 PHP 怎么样把一张图片缩小到指定大小 php图片可以等比例的缩放吗可以。
等比例缩放的方法是:
1、载入选区--自由变换。如下图:
2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。
php实现图片等比例缩放代码新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)
源代码如下:
<?php
$filename="q.jpg";
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imagecopyresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//图像输出新图片、另存为
imagejpeg($new,
"q1.jpg");
imagedestroy($new);
imagedestroy($img);
?>
使用浏览器运行过后,在index.php同级的目录下会有个q1.jpg,这个图片就是等比例缩放后的图片,路径可以自己在源代码里面更改,放在自己的项目当中去或写个方法也行
以上所述上就是本文的全部内容了,希望对大家学习php语言能够有所帮助。
求php图片缩放处理函数<?php
/**
* 图片缩放
* @param string $url
* @param int $maxWidth
* @param int $maxHeight
* @return string
*/
function thumb($url, $maxWidth, $maxHeight, $info) {
$info = $imgInfo = getimagesize($url);
$width = $imgInfo[0];//获取图片宽度
$height = $imgInfo[1];//获取图片高度
$r = min($maxHeight/$height, $maxWidth/$width);
if($r >= 1) { // 不用缩放
$maxHeight = $height;
$maxWidth = $width;
} elseif($r < 1) { // 缩放
$maxHeight = $height * $r;
$maxWidth = $width * $r;
}
$temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //创建画布
$fun = str_replace('/', 'createfrom', $imgInfo['mime']);
$im = $fun($url);
imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun = str_replace('/', '', $imgInfo['mime']);
$fun($temp_img);
$imgstr = ob_get_contents();
ob_end_clean();
imagedestroy($im);
return $imgstr;
}
$imgUrl = $_GET['url'];
$info = array();
$string = thumb($imgUrl, 500, 500, $info);
$mimeArray = explode("/", $info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo $string;
以上代码存为thumb.php,调用效果:
php 怎么压缩图片的大小php 压缩图片的大小:
<?php
$im = imagecreatefromjpeg('D:phpplace.jpeg');
resizeImage($im,,,'xinde','.jpg');
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
echo "start-----------------" ;
if(($maxwidth $pic_width > $maxwidth) ($maxheight $pic_height > $maxheight))
{
if($maxwidth $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
PHP 怎么样把一张图片缩小到指定大小如果是改变显示的大小,直接img标签属性里,width和height设置啊。
如果想真正改变,你看看这个代码(没试验过):
function makeThumb($srcFile,$dstFile,$dstW,$dstH) {
$data=GetImageSize($srcFile,$info);
switch (CoreUtil::getFileExtension($dstFile)){
case'gif':
$im= @ImageCreateFromGIF($srcFile); break;
case'jpg':
case'jpeg':
$im= @imagecreatefromjpeg($srcFile); break;
case'png':
$im= @ImageCreateFromPNG($srcFile); break;
default:returnFalse;
}
if(!$im) returnFalse;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW){
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2); $fdstW=$dstW;
} else {
$fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$ni=ImageCreate($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$black= ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$black);
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
ImageJpeg($ni,$dstFile);
imagedestroy($im);
imagedestroy($ni);
returnTrue;
}
大概就是用到imagecreatefromjpeg、imagecreatetruecolor、imagecopyresampled 、 imagepng这几个函数
关于php图片放大缩小的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图片放大缩小 php图片压缩的详细内容...