96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

php图片压缩成指定大小-PHP 怎么样把一张图片缩小到指定大小

原创:找图网 2023-05-03 17:25:53
  • php 怎么压缩图片的大小

  • php 压缩图片的大小:

    <?php

    $im = imagecreatefromjpeg('D:');

    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 怎么压缩图片的大小

  • php 压缩图片的大小:

    <?php

    $im = imagecreatefromjpeg('D:');

    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这几个函数

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com