php產生縮圖的效能

來源:互聯網
上載者:User
php產生縮圖的功能
客戶自己上傳的圖片大小不標準,想讓傳上來的圖片統一變成150X113尺寸的。從網上搜到的程式在IE裡用正常,到了別的瀏覽器(如搜狗瀏覽器等)就不行,這是什麼原因?(感覺老是不執行)
代碼如下:

session_start();
if (isset($_SESSION['adminname']) && isset($_SESSION['adminpass'])){


//****************************************
//產生縮圖========================================
$myname=date("YmdHis");

$filename="product/min/".$myname; //小圖片檔案名稱
$resizewidth=150; // 產生圖片的寬度
$resizeheight=113; // 產生圖片的高度


function ResizeImage($im,$maxwidth,$maxheight,$name){
$resizewidth = $resizeheight = false;

$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if($resizewidth && $resizeheight){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($resizewidth){
$ratio = $widthratio;
}elseif($resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}



if(isset($_FILES['image']['size'])){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}


if($im){
if(file_exists("$filename.jpg")){
unlink("$filename.jpg");
}

ResizeImage($im,$resizewidth,$resizeheight,$filename); //產生小圖
ImageDestroy ($im);

echo "上傳成功,圖片檔案名稱是:";
echo $myname.".jpg";
}
}

//****************************************
}else{
echo "";
}
?>











------解決思路----------------------
你看看 product/min/ 目錄下是否有產生的圖片
------解決思路----------------------
ie ,Firefox,chrome試試看,這三個行的話說明代碼沒問題。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.