如何利用函式,將上傳後的圖片自動縮小製成新圖片

Home Home
引用 | 編輯 ruby0305
2007-06-12 17:47
樓主
推文 x0
這是在試驗把伺服器上已存在的圖片,自動新製一個縮小後的圖片

我一直錯在Imagecopyresized()這行。查了很久也查不到原因。
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /hd2/home/frankie/_www_/bnw_web/test_events.php on line 94

而且我覺得,這函式並不是我真正的需求,只是我找不到更適合的,請各位前輩教教我,有那些函式可以
滿足我的需求。

需求:上傳圖片後(已做好)程式自動再製一張縮小圖,另存新檔,存檔的地方可以選擇的話是較好的
拜託各位大大了,我真的找好久,找不到適合的函式可以用。謝謝

複製程式
              $size=GetImageSize("promo_img/".$pic_name);
              if (sizeof($size) !="")
              {
                     $old_width = $size[0];
                     $old_height = $size[1];
              
                     $pic_range =        $size[0] /160;
                     if($pic_range != 0)
                     {
                            $width =  $size[0] /$pic_range;
                            $height =  $size[1] /$pic_range;
                     }       
                     // 下面兩個變數分別儲存縮圖的寬度與高度 
                     $newWidth = $width ;
                     $newHeight = $height; 
                     echo $newWidth ."&&". $newHeight ."&&". $old_width ."&&". $old_height."____________<br>";

                     // 讀入原來大圖片的資料 
                     $origImg = "promo_img/aa.jpg"; 
              $newImg = $article_sn .$origImg;
                     $newImg =              ImageCreateFromJPEG($origImg);  
                     ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,$old_width,$old_height);               
                     ImageJPEG($newImg);
                     ImageDestroy($newImg);                     
              }


獻花 x0
引用 | 編輯 ruby0305
2007-06-14 17:02
1樓
  
我找到方法了,雖然沒有前輩指點,但我還是說聲謝謝......... 表情

獻花 x0
引用 | 編輯 笑笑
2007-06-15 09:07
2樓
  
這個方法可以把圖縮小,不過品質會比較不好,如果主機有 ImageMagick 支援,用 ImageMagick 去縮小,圖會比較好看..

複製程式
<?php 
$w="240"; //自行設定的縮圖寬度
$h="320"; //自行設定的縮圖高度
if($_FILES['file']['type']=="image/pjpeg" || $_FILES['file']['type']=="image/gif" || $_FILES['file']['type']=="image/png") {
if($_FILES['file']['type']=="image/pjpeg") {
$filename=substr(rand(0,999999999999),1,7).".gif"; //隨機取檔名..我的習慣啦 ^^||
}
if($_FILES['file']['type']=="image/gif") {
$filename=substr(rand(0,999999999999),1,7).".gif";
}
if($_FILES['file']['type']=="image/png") {
$filename=substr(rand(0,999999999999),1,7).".png";
}
$size=$w."x".$h;
copy($_FILES['file']['tmp_name'],"/home/caisse/case/kf/data/file/b".$filename); //將暫存檔copy至自己定義的目錄..因為小弟大小圖都想要 ^^
$big_image="/home/caisse/case/kf/data/file/b".$filename;
$small_image="/home/caisse/case/kf/data/file/s".$filename;
$exec_str="/usr/bin/convert '-geometry' ".$size." ".$big_image." ".$small_image; //注意"跟'唷
exec($exec_str);
} else {
echo "請上傳圖片";
}
?>
<form action="#" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="送出">
</form>
<?php if($_FILES) { ?>
原始圖 <img src="/data/file/b<?php echo $filename; ?>"> <br>
裁切後的圖 <img src="/data/file/s<?php echo $filename; ?>"> 
<?php } ?>

轉載自 http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=912

獻花 x0