基于PHP實現(xiàn)等比壓縮圖片大小
更新時間:2016年03月04日 11:57:24 作者:zpercx
通過本段代碼給大家介紹基于php實現(xiàn)等比壓縮圖片大小的相關(guān)知識,代碼簡單易懂,對php壓縮圖片相關(guān)知識感興趣的朋友參考下吧
廢話不多說了,直接給大家貼php等比壓縮圖片大小的相關(guān)代碼了,具體代碼如下所示:
<?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);
}
}
以上代碼內(nèi)容是小編給大家介紹的基于PHP實現(xiàn)等比壓縮圖片大小的相關(guān)內(nèi)容,代碼簡單易懂,哪里寫的不好,歡迎各位大俠多多提出寶貴意見,小編非常樂意。
相關(guān)文章
destoon安裝出現(xiàn)Internal Server Error的解決方法
這篇文章主要介紹了destoon安裝出現(xiàn)Internal Server Error的解決方法,需要的朋友可以參考下2014-06-06
C#使用PHP服務(wù)端的Web Service通信實例
這篇文章主要介紹了C#使用PHP服務(wù)端的Web Service通信實例,需要的朋友可以參考下2014-04-04
php解決搶購秒殺抽獎等大流量并發(fā)入庫導(dǎo)致的庫存負數(shù)的問題
最近在做一個團購項目,遇到個問題,就是在搶購、秒殺、抽獎等活動時,庫存數(shù)量有限,但是同時下單人數(shù)超過了庫存數(shù)量,就會導(dǎo)致商品超售問題。那么我們怎么來解決這個問題呢,我的思路如下:2014-06-06
Swoole-1.7.22 版本已發(fā)布,修復(fù)PHP7相關(guān)問題
swoole-1.7.22 版本已發(fā)布,此版本是一個BUG修復(fù)版本,專門針對PHP7做了大量修改,可完美運行于PHP7環(huán)境2015-12-12
PHP產(chǎn)生不重復(fù)隨機數(shù)的5個方法總結(jié)
這篇文章主要介紹了PHP產(chǎn)生不重復(fù)隨機數(shù)的5個方法總結(jié),PHP隨機數(shù)經(jīng)常在項目中使用,本文總結(jié)了網(wǎng)絡(luò)上的和自己項目中用到的隨機數(shù)生成方法,需要的朋友可以參考下2014-11-11

