10/10/2018, 10:51
Hàm upload hình ảnh ntn để đỡ tốn BW và CPU usage?
Ai tư vẫn giúp, mình có viết cái này nhưng thấy ngốn % CPU...
Mình có hàm thế này!
Mình có hàm thế này!
Code:
function imgupload($file, $name)
{
$image = $file['name'];
$uploadedfile = $file['tmp_name'];
if (!getimagesize($uploadedfile)) {
echo 'Loi upload hinh anh!';
return false;
}
if ($image)
{
$filename = stripslashes($file['name']);
$extension = $this->getExtension($filename);
$extension = strtolower($extension);
if (($extension != 'jpg') && ($extension != 'jpeg')
&& ($extension != 'png') && ($extension != 'gif'))
{
echo 'Ko dung dinh dang';
return false;
}
else
{
$fsize = $file['size'];
if($extension=='jpg' || $extension=='jpeg' )
{
$uploadedfile = $file['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=='png')
{
$uploadedfile = $file['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else $src = imagecreatefromgif($uploadedfile);
$infos = getimagesize($uploadedfile);
$max = 800;
$maxthumb = 160;
$quatily = 80;
$new = $this->resize($infos, $max, $max);
$tmp = imagecreatetruecolor($new->w,$new->h);
$newthumb = $this->resize($infos, $maxthumb, $maxthumb);
$tmpthumb = imagecreatetruecolor($newthumb->w,$newthumb->h);
imagecopyresampled($tmp,$src,0,0,0,0,$new->w,$new->h,
$infos[0],$infos[1]);
imagecopyresampled($tmpthumb,$src,0,0,0,0,$newthumb->w,$newthumb->h,
$infos[0],$infos[1]);
$photoPath = $this->photoPath;
$thumbsPath = $this->thumbsPath;
$this->createfolder($photoPath);
$this->createfolder($thumbsPath);
$photoPath = $this->photoPath.DS.$name;
$thumbsPath = $this->thumbsPath.DS.$name;
imagejpeg($tmp,$photoPath,$quatily);
imagejpeg($tmpthumb,$thumbsPath,$quatily);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmpthumb);
}
}
return true;
}
function resize($infos, $width, $height)
{
//Don't resize images which are smaller than thumbs
if ($infos[0] < $width && $infos[1] < $height) {
$iNew->w = $infos[0];
$iNew->h = $infos[1];
return $iNew;
}
// keep proportions
$iWidth = $infos[0];
$iHeight = $infos[1];
$iRatioW = $width / $iWidth;
$iRatioH = $height / $iHeight;
if ($iRatioW < $iRatioH) {
$iNew->w = $iWidth * $iRatioW;
$iNew->h = $iHeight * $iRatioW;
} else {
$iNew->w = $iWidth * $iRatioH;
$iNew->h = $iHeight * $iRatioH;
}
return $iNew;
}
Bài liên quan





[=========> Bổ sung bài viết <=========]
upp! cần đc giải quyết!
Nên cách này không ổn đâu