求PHP產生縮圖的功能,該怎麼解決

來源:互聯網
上載者:User
求PHP產生縮圖的功能
求PHP產生縮圖的功能!

從網上找了一段代碼,產生的縮圖色彩失真。

------解決方案--------------------
31.$image1=$info['filename'].'120_120'.'.' .$info['extension'];//縮圖名稱
32.$image2=$info['filename'].'300_300'.'.' .$info['extension'];//縮圖名稱
33.list($imagw,$imagh)=getimagesize($file ['tmp_name']);//取得原圖的寬高(另外$file ['tmp_name']只是一個臨時檔案路徑,有時可能 會報錯,如報錯只需把臨時檔案路徑改成你上傳 後的原圖的路徑即可)
34.//120圖判斷各種情況來取得縮圖的寬高
35.if($imagw< =120&&$imagh< =120){
36.$m120w=$imagw;
37.$m120h=$imagh;
38.}elseif($imagw>$imagh)
39.{
40.$m120w=120;
41.$m120h=intval($m120w/number_format( $imagw/$imagh,2));//縮圖的寬除以(原 圖的寬除以原圖的高)number_format查手冊
42.}
43.else
44.{
45.$m120h=300;
46.$m120w=intval($m120h/number_format( $imagh/$imagw,2));//縮圖的高除以(原圖 的高除以原圖的寬)number_format查手冊
47.}
48.//300圖判斷各種情況來取得縮圖的寬高
49.if($imagw< =300&&$imagh< =300){
50.$m300w=$imagw;
51.$m300h=$imagh;
52.}elseif($imagw>$imagh){
53.$m300w=300;
54.$m300h=intval($m300w/number_format( $imagw/$imagh,2));//縮圖的寬除以(原 圖的寬除以原圖的高)number_format查手冊
55.}else{
56.$m300h=300;
57.$m300w=intval($m300h/number_format( $imagh/$imagw,2));//縮圖的高除以(原 圖的高除以原圖的寬)number_format查手冊
58.}
59.if($info['extension']=='jpg'){
60.$info['extension']='jpeg';//判斷映像類 型如果是JPG的則把它轉換成JPEG的因為映像處 理函數在處理JPG的時候都是用的JPEG而不是JPG
61.}
62.$iml='imagecreatefrom'.$info['extension'];
63.//取得不同圖片格式的不同函數
64.$yuan=$iml($file['tmp_name']);
65.//根據前面取的不同函數來取的原圖的標識
66.$mu1=imagecreatetruecolor($m120w,$m120h);
67.//取的要產生的縮圖的標識
68.$mu2=imagecreatetruecolor($m300w,$m300h);
69.//取的要產生的縮圖的標識
70.$c='image'.$info['extension'];
71.//產生輸出縮圖的函數,不能分開寫,否則報錯
72.imagecopyresampled($mu1,$yuan,0,0,0, 0,$m120w,$m120h,$imagw,$imagh);//產生縮圖120
73.$res=$c($mu1,$xin.'/'.$image1);
74.//存放縮圖
75.if(!$res){
76.alert('產生120縮圖失敗');
77.exit;
78.}
79.imagecopyresampled($mu2,$yuan,0,0 ,0,0,$m300w,$m300h,$imagw,$imagh);
80.//PHP產生縮圖產生縮圖120
81.$res=$c($mu2,$xin.'/'.$image2);
82.//存放縮圖
83.if($res){
84.alert('產生300縮圖成功');
這裡只列出了一部分代碼,具體的參考:http://www.phpnewer.com/index.php/Ymgx/detail/id/38
------解決方案--------------------
試下這個
1.< ?php
2./*後台登陸介面
3.*黃春龍學習編寫
4.*首寫時間:2009-10-27
5.*更新時間:2009/11/24
6.*/
7.require_once '../xmphp/init.php';
8.//全站設定檔
9.require_once XMPHP_COMMON.'/smarty.php';
10.//smarty功能
11.require_once XMPHP_COMMON.'/mysql.php';
12.//mysql功能
13.require_once XMPHP_COMMON.'/func.php';
14.//常用函數
15.if(isset($_GET['act'])&&$_GET['act'] =='upload'){
16.$file=$_FILES['file'];
17.$info=pathinfo($file['name']);
18.$a=array('jpg','gif','png');
19.if(!in_array($info['extension'],$a)){
20.alert("請選擇jpg,gif,png格式的圖片 進行上傳");
21.exit;
22.}
23.if($file['name'] !=''){
24.$dirname=date('Ym');
25.$dirname='../upload/'.$dirname;
26.//原圖路徑
27.$xin=$dirname.'/s';//PHP產生縮圖路徑
28.if(!file_exists($xin)){
29.@mkdir($xin);
30.}
31.$image1=$info['filename'].'120_120'.'.' .$info['extension'];//縮圖名稱
32.$image2=$info['filename'].'300_300'.'.' .$info['extension'];//縮圖名稱
33.list($imagw,$imagh)=getimagesize($file ['tmp_name']);//取得原圖的寬高(另外$file ['tmp_name']只是一個臨時檔案路徑,有時可能 會報錯,如報錯只需把臨時檔案路徑改成你上傳 後的原圖的路徑即可)
34.//120圖判斷各種情況來取得縮圖的寬高
35.if($imagw< =120&&$imagh< =120){
36.$m120w=$imagw;
37.$m120h=$imagh;
38.}elseif($imagw>$imagh)
39.{
40.$m120w=120;
41.$m120h=intval($m120w/number_format( $imagw/$imagh,2));//縮圖的寬除以(原 圖的寬除以原圖的高)number_format查手冊
42.}
43.else
44.{
45.$m120h=300;
46.$m120w=intval($m120h/number_format( $imagh/$imagw,2));//縮圖的高除以(原圖 的高除以原圖的寬)number_format查手冊
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.