PHP-ピクチャ圧縮
15356 ワード
方法1:
方式2:
<?php
/*
----------------------------------------------------------------------
:
:2009-8-8
:True/False
:
$Image ( )
$Dw=450 ;
$Dh=450 ;
$Type=1 1, ; 2,
:Seven(QQ:9256114)WWW.7DI.NET*/
$path='img/';//
$phtypes=array(
'img/gif',
'img/jpg',
'img/jpeg',
'img/bmp',
'img/pjpeg',
'img/x-png'
);
Function Img($Image,$Dw=450,$Dh=450,$Type=1){
IF(!File_Exists($Image)){
Return False;
}
# , $Image
IF($Type!=1){
Copy($Image,Str_Replace(".","_x.",$Image));
$Image=Str_Replace(".","_x.",$Image);
}
# ,
$ImgInfo=GetImageSize($Image);
Switch($ImgInfo[2]){
Case 1:
$Img = @ImageCreateFromGIF($Image);
Break;
Case 2:
$Img = @ImageCreateFromJPEG($Image);
Break;
Case 3:
$Img = @ImageCreateFromPNG($Image);
Break;
}
# ,
IF(Empty($Img)){
# ,
IF($Type!=1){Unlink($Image);}
Return False;
}
#
IF($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
}ElseIF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
}Else{
$width=$width;
$height=$height;
}
$nImg = ImageCreateTrueColor($width,$height); #
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);#
ImageJpeg ($nImg,$Image); # JPEG
Return True;
#
}Else{
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
$nImg = ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){ #
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
}Else{ #
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
}
ImageJpeg ($nImg,$Image);
Return True;
}
}
?>
<html><body>
<form method="post" enctype="multipart/form-data" name="form1">
<table>
<tr><td> </td></tr>
<tr><td><input type="file" name="photo" size="20" /></td></tr>
<tr><td><input type="submit" value=" "/></td></tr>
</table>
:<?=implode(', ',$phtypes)?></form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if (!is_uploaded_file($_FILES["photo"][tmp_name])){
echo " ";
exit();
}
if(!is_dir('img')){//
mkdir('img');
}
$upfile=$_FILES["photo"];
$pinfo=pathinfo($upfile["name"]);
$name=$pinfo['basename'];//
$tmp_name=$upfile["tmp_name"];
$file_type=$pinfo['extension'];//
$showphpath=$path.$name;
if(in_array($upfile["type"],$phtypes)){
echo " !";
exit();
}
if(move_uploaded_file($tmp_name,$path.$name)){
echo " !";
Img($showphpath,100,800,2);
}
echo "<img src=\"".$showphpath."\" />";
}
?>
</body>
</html>
方式2:
PHP
<?php
class ThumbHandler
{
var $dst_img;//
var $h_src; //
var $h_dst;//
var $h_mask;//
var $img_create_quality = 100;//
var $img_display_quality = 80;// , 75
var $img_scale = 0;//
var $src_w = 0;//
var $src_h = 0;//
var $dst_w = 0;//
var $dst_h = 0;//
var $fill_w;//
var $fill_h;//
var $copy_w;//
var $copy_h;//
var $src_x = 0;//
var $src_y = 0;//
var $start_x;//
var $start_y;//
var $mask_word;//
var $mask_img;//
var $mask_pos_x = 0;//
var $mask_pos_y = 0;//
var $mask_offset_x = 5;//
var $mask_offset_y = 5;//
var $font_w;//
var $font_h;//
var $mask_w;//
var $mask_h;//
var $mask_font_color = "#ffffff";//
var $mask_font = 2;//
var $font_size;//
var $mask_position = 0;//
var $mask_img_pct = 50;// , ,
var $mask_txt_pct = 50;// , ,
var $img_border_size = 0;//
var $img_border_color;//
var $_flip_x=0;//
var $_flip_y=0;//
var $cut_type=0;//
var $img_type;//
// ,
var $all_type = array(
"jpg" => array("output"=>"imagejpeg"),
"gif" => array("output"=>"imagegif"),
"png" => array("output"=>"imagepng"),
"wbmp" => array("output"=>"image2wbmp"),
"jpeg" => array("output"=>"imagejpeg"));
/**
*
*/
function ThumbHandler()
{
$this->mask_font_color = "#ffffff";
$this->font = 2;
$this->font_size = 12;
}
/**
*
*/
function getImgWidth($src)
{
return imagesx($src);
}
/**
*
*/
function getImgHeight($src)
{
return imagesy($src);
}
/**
*
*
* @param string $src_img
*/
function setSrcImg($src_img, $img_type=null)
{
if(!file_exists($src_img))
{
die(" ");
}
if(!empty($img_type))
{
$this->img_type = $img_type;
}
else
{
$this->img_type = $this->_getImgType($src_img);
}
$this->_checkValid($this->img_type);
// file_get_contents php >4.3.0
$src = '';
if(function_exists("file_get_contents"))
{
$src = file_get_contents($src_img);
}
else
{
$handle = fopen ($src_img, "r");
while (!feof ($handle))
{
$src .= fgets($fd, 4096);
}
fclose ($handle);
}
if(empty($src))
{
die(" ");
}
$this->h_src = @ImageCreateFromString($src);
$this->src_w = $this->getImgWidth($this->h_src);
$this->src_h = $this->getImgHeight($this->h_src);
}
/**
*
*
* @param string $dst_img
*/
function setDstImg($dst_img)
{
$arr = explode('/',$dst_img);
$last = array_pop($arr);
$path = implode('/',$arr);
$this->_mkdirs($path);
$this->dst_img = $dst_img;
}
/**
*
*
* @param string $n
*/
function setImgDisplayQuality($n)
{
$this->img_display_quality = (int)$n;
}
/**
*
*
* @param string $n
*/
function setImgCreateQuality($n)
{
$this->img_create_quality = (int)$n;
}
/**
*
*
* @param string $word
* @param integer $font
* @param string $color
*/
function setMaskWord($word)
{
$this->mask_word .= $word;
}
/**
*
*
* @param string $color
*/
function setMaskFontColor($color="#ffffff")
{
$this->mask_font_color = $color;
}
/**
*
*
* @param string|integer $font
*/
function setMaskFont($font=2)
{
if(!is_numeric($font) && !file_exists($font))
{
die(" ");
}
$this->font = $font;
}
/**
* , truetype
*/
function setMaskFontSize($size = "12")
{
$this->font_size = $size;
}
/**
*
*
* @param string $img
*/
function setMaskImg($img)
{
$this->mask_img = $img;
}
/**
*
*
* @param integer $x
*/
function setMaskOffsetX($x)
{
$this->mask_offset_x = (int)$x;
}
/**
*
*
* @param integer $y
*/
function setMaskOffsetY($y)
{
$this->mask_offset_y = (int)$y;
}
/**
*
*
* @param integer $position ,1: ,2: ,3: ,0/4:
*/
function setMaskPosition($position=0)
{
$this->mask_position = (int)$position;
}
/**
*
*
* @param integer $n
*/
function setMaskImgPct($n)
{
$this->mask_img_pct = (int)$n;
}
/**
*
*
* @param integer $n
*/
function setMaskTxtPct($n)
{
$this->mask_txt_pct = (int)$n;
}
/**
*
*
* @param ( ) ( ) ( )
*/
function setDstImgBorder($size=1, $color="#000000")
{
$this->img_border_size = (int)$size;
$this->img_border_color = $color;
}
/**
*
*/
function flipH()
{
$this->_flip_x++;
}
/**
*
*/
function flipV()
{
$this->_flip_y++;
}
/**
*
*
* @param ( ) ( ) ( )
*/
function setCutType($type)
{
$this->cut_type = (int)$type;
}
/**
*
*
* @param integer $width
*/
function setRectangleCut($width, $height)
{
$this->fill_w = (int)$width;
$this->fill_h = (int)$height;
}
/**
*
*
* @param ( ) ( ) ( )
*/
function setSrcCutPosition($x, $y)
{
$this->src_x = (int)$x;
$this->src_y = (int)$y;
}
/**
* ,
* @param integer $a , ,
*
* @param integer $b
*/
function createImg($a, $b=null)
{
$num = func_num_args();
if(1 == $num)
{
$r = (int)$a;
if($r < 1)
{
die(" 1");
}
$this->img_scale = $r;
$this->_setNewImgSize($r);
}
if(2 == $num)
{
$w = (int)$a;
$h = (int)$b;
if(0 == $w)
{
die(" 0");
}
if(0 == $h)
{
die(" 0");
}
$this->_setNewImgSize($w, $h);
}
if($this->_flip_x%2!=0)
{
$this->_flipH($this->h_src);
}
if($this->_flip_y%2!=0)
{
$this->_flipV($this->h_src);
}
$this->_createMask();
$this->_output();
//
if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))
{
Return true;
}
else
{
Return false;
}
}
/**
* ,
*/
function _createMask()
{
if($this->mask_word)
{
//
$this->_setFontInfo();
if($this->_isFull())
{
die(" ");
}
else
{
$this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
$white = ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//
$this->_drawBorder();
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x, $this->src_y,
$this->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);
$this->_createMaskWord($this->h_dst);
}
}
if($this->mask_img)
{
$this->_loadMaskImg();// ,
if($this->_isFull())
{
//
$this->_createMaskImg($this->h_src);
$this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
$white = ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//
$this->_drawBorder();
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x, $this->src_y,
$this->fill_w, $this->start_y,
$this->copy_w, $this->copy_h);
}
else
{
//
$this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
$white = ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//
$this->_drawBorder();
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x, $this->src_y,
$this->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);
$this->_createMaskImg($this->h_dst);
}
}
if(empty($this->mask_word) && empty($this->mask_img))
{
$this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
$white = ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//
$this->_drawBorder();
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x, $this->src_y,
$this->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);
}
}
/**
*
*/
function _drawBorder()
{
if(!empty($this->img_border_size))
{
$c = $this->_parseColor($this->img_border_color);
$color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);//
}
}