[PHP]用來-轉檔-縮圖-輸出

一些網路上收集的涵式

<?php
class resize_img
{
var $image_path = '';
//holds the image path
var $sizelimit_x = 250;
//the limit of the image width
var $sizelimit_y = 250;
//the limit of the image height
var $image_resource = '';
//holds the image resource
var $keep_proportions = true;
//if true it keeps the image proportions when resized
var $resized_resource = '';
//holds the resized image resource
var $hasGD = false;
var $output = 'SAME';
//can be JPG, GIF, PNG, or SAME (same will save as old type)

function resize_img()
{
if( function_exists('gd_info') ){ $this-&gt;hasGD = true; }  //---判斷GD函式是否支援
}

function resize_image( $image_path )
{
if( $this-&gt;hasGD === false ){ return false; }
//沒有GD支援

list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );
//這是會得到的圖像的寬度,高度和格式

if( ( $img_width != 0 ) || ( $img_width != 0 ) )
//確保它是正確裝入
{
switch( $img_type )
{
case 1:
//GIF
$this-&gt;image_resource = @imagecreatefromgif( $image_path );
if( $this-&gt;output == 'SAME' ){ $this-&gt;output = 'GIF'; }
break;
case 2:
//JPG
$this-&gt;image_resource = @imagecreatefromjpeg( $image_path );
if( $this-&gt;output == 'SAME' ){ $this-&gt;output = 'JPG'; }
break;
case 3:
//PNG
$this-&gt;image_resource = @imagecreatefrompng( $image_path );
if( $this-&gt;output == 'SAME' ){ $this-&gt;output = 'PNG'; }
}
if( $this-&gt;image_resource === '' ){ return false; }
//它無法加載圖像
}
else{ return false; }
//發生無法預期的錯誤

if( $this-&gt;keep_proportions === true )
{
if( ($img_width-$this-&gt;sizelimit_x) &gt; ($img_height-$this-&gt;sizelimit_y) )
{
//如果IMG寬度 大於規定的大小限制,我們的寬度將做縮減
$scalex = ( $this-&gt;sizelimit_x / $img_width );
$scaley = $scalex;
}
else
{
//如果IMG高度 大於規定的大小限制,我們的高度將做縮減
$scalex = ( $this-&gt;sizelimit_y / $img_height );
$scaley = $scalex;
}

}
else
{
$scalex = ( $this-&gt;sizelimit_x / $img_width );
$scaley = ( $this-&gt;sizelimit_y / $img_height );
//只是使圖像適合圖像的大小限制

if( $scalex &gt; 1 ){ $scalex = 1; }
if( $scaley &gt; 1 ){ $scaley = 1; }
//圖像小於限制所以不做變更
}

$new_width = $img_width * $scalex;//計算新的寬
$new_height = $img_height * $scaley;//計算新的高

$this-&gt;resized_resource = @imagecreatetruecolor( $new_width, $new_height );
//創建一個圖像資源,用寬度和高度的大小限制(或新的調整比例)

if( function_exists( 'imageantialias' )){ @imageantialias( $this-&gt;resized_resource, true ); }
//有助於在質量的圖像被調整判斷是否有此函式

@imagecopyresampled( $this-&gt;resized_resource, $this-&gt;image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );
//調整張圖片上調整資源

@imagedestroy( $this-&gt;image_resource );
//清空舊的圖像資源

return true;
}
//判斷輸出為什麼格式
function save_resizedimage( $path, $name )
{
switch( strtoupper($this-&gt;output) )
{
case 'GIF':
//GIF
@imagegif( $this-&gt;resized_resource, $path . $name . '.gif' );
break;
case 'JPG':
//JPG
@imagejpeg( $this-&gt;resized_resource, $path . $name . '.jpg' );
break;
case 'PNG':
//PNG
@imagepng( $this-&gt;resized_resource, $path . $name . '.png' );
}
}
//--此函式用來在頁面顯示圖片
function output_resizedimage()
{
$the_time = time();
header('Last-Modified: ' . date( 'D, d M Y H:i:s', $the_time ) . ' GMT');
header('Cache-Control: public');

switch( strtoupper($this-&gt;output) )
{
case 'GIF':
//GIF
header('Content-type: image/gif');
@imagegif( $this-&gt;resized_resource );
break;
case 'JPG':
//JPG
header('Content-type: image/jpg');
@imagejpeg( $this-&gt;resized_resource );
break;
case 'PNG':
//PNG
header('Content-type: image/png');
@imagepng( $this-&gt;resized_resource );
}
}

function destroy_resizedimage()
{
@imagedestroy( $this-&gt;resized_resource );
@imagedestroy( $this-&gt;image_resource );
}

}
?>
[/php]



[php]
/*範例-----可以用來-轉檔-縮圖-輸出

$imgresize = new resize_img();

$imgresize-&gt;sizelimit_x = 100;
$imgresize-&gt;sizelimit_y = 100;
$imgresize-&gt;keep_proportions = true;
$imgresize-&gt;output = 'GIF';

$filename = uniqid().rand(1000,9999);

if( $imgresize-&gt;resize_image( $_FILES['Filedata']['tmp_name'] ) === false )
{
echo 'ERROR!';
}
else
{
$imgresize-&gt;save_resizedimage( 'D:/www/test/thumbnails/', $filename );
}

$imgresize-&gt;destroy_resizedimage();

*/

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *