The friend who makes the picture kind of website may have own way of uploading the picture. Here to introduce a PHP upload image class, it can verify whether the upload file is a picture, the format of the image is acceptable to the site, you can also generate a specified size of the thumbnail image. The exact method is as follows:
<?php
Class Uploadcomponent {
var $imageData = array ();
var $destinationDir = NULL;
var $thumbDestinationDir = NULL;
var $imageSize = array (500,500);
var $cropImageSize = array (90,90);
var $tag = ' width ';
var $imageExt = array (' jpg ', ' gif ', ' PNG ', ' BMP ', ' jpeg ');
Function Initialize ($arr)
{
$this->imagedata = $arr;
}
function Isimagefile ()
{
$ext = $this->imageextension ();
if (In_array (Strtolower ($ext), $this->imageext))
{
return true;
}
Else
{
return false;
}
}
function Isvalidimage ()
{
if ($this->imagedata[' error '] = = 0 && $this->imagedata[' name '] <> ")
{
if ($this->isimagefile ())
{
return true;
}
Else
{
return false;
}
}
Else
{
return false;
}
}
function Imageextension ()
{
Return Trim (substr ($this->imagedata[' name '],strpos ($this->imagedata[' name '], '. ') +1,strlen ($this->imagedata[' name ')));
}
function upload ($thumb =false)
{
$this->__upload ($this->destinationdir,$ This->imagesize[0], $this->imagesize[1]);
if ($thumb)
{
$this->__upload ($this, Thumbdestinationdir, $this->cropimagesize[0], $this->cropimagesize[1]);
}
}
function __upload ($filethumb, $Twidth, $Theight)
{
list ($width, $height, $type, $ attr) =getimagesize ($this->imagedata[' tmp_name ');
switch ($type)
{
case 1:
$img = Imagecreatefromgif ($this->imagedata[' tmp_name ');
break;
case 2:
$img =imagecreatefromjpeg ($this->imagedata[' Tmp_name ‘]);
break;
case 3:
$img =imagecreatefrompng ($this->imagedata[' Tmp_name ' ]);
break;
}
if ($this->tag = = "width")//Compress pictures by width
{
$Theight =round (($ height/$width) * $Twidth);
}
elseif ($this->tag = = "height")//Compress pictures by height
{
$Twidth =round (($width/$height) * $Theight);
}
else
{
if ($width > $height)
$ Theight=round (($height/$width) * $Twidth);
else
$Twidth =round (($width/$height) * $Theight);
}
$thumb =imagecreatetruecolor ($Twidth, $Theight);
if (imagecopyresampled ($thumb, $img, 0,0,0,0, $Twidth, $Theight, $width, $height))
{
Switch ($type)
{
Case 1:
Imagegif ($thumb, $filethumb);
Break
Case 2:
Imagejpeg ($thumb, $filethumb, 100);
Break
Case 3:
Imagepng ($thumb, $filethumb);
Break
}
chmod ($filethumb, 0666);
return true;
}
}
}
?>
The use of this class is very simple, the upload file $_files[' HTML form of the upload item name '] This variable as an instantiation of this class variable passed to the constructor. You can then use one of the methods to process the picture. When using the upload ($thumb =false) function, $thumb =false to not generate thumbnails, and vice versa to generate thumbnails, and the size of pictures and thumbnails can be specified by $imageSize, $cropImageSize these two variables. The default is 500*500 and 90*90, friends can change them according to their own needs.
PHP upload image class to generate thumbnail images