private function cropImg($Image, $Dw, $Dh, $Type = 2) { if ($Type != 1) { $tmp = realpath(APPPATH . '../upload') . '/' . str_replace('.', '_' . $Dw . '.', $Image); $Image = realpath(APPPATH . '../upload') . '/' . $Image; copy($Image, $tmp) or die('error'); $Image = $tmp; } if (!file_exists($Image)) { echo "不存在图片"; return false; } $ImgInfo = getimagesize($Image); switch ($ImgInfo[2]) { case 1: $fn = 'imagegif'; $im = @imagecreatefromgif($Image); break; case 2: $fn = 'imagejpeg'; $im = @imagecreatefromjpeg($Image); break; case 3: $fn = 'imagepng'; $im = @imagecreatefrompng($Image); break; default: echo "格式不支持"; return false; } $w = ImagesX($im); $h = ImagesY($im); $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); imagealphablending($nImg, false); imagesavealpha($nImg, true); ImageCopyReSampled($nImg, $im, 0, 0, 0, 0, $width, $height, $w, $h); $fn($nImg, $Image); return true; }
function Img($Image, $Dw = 200, $Dh = 300, $Type = 2) { if (!File_Exists($Image)) { return False; } #如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值 // IF($Type!=1){ Copy($Image, Str_Replace(".", "_x.", $Image)); $Image = Str_Replace(".", "_x.", $Image); // } var_dump($Image); #取得文件的类型,根据不同的类型建立不同的对象 $ImgInfo = GetImageSize($Image); var_dump($ImgInfo); 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; } }
ImageSaveAlpha($thumb, true); //这里很重要,意思是不要丢了$thumb图像的透明色; $color = imagecolorallocatealpha($thumb, 0, 0, 0, 127); ImageFill($thumb, 0, 0, $color); ImageColorTransparent($thumb, $color); } else { $white = ImageColorAllocate($thumb, 255, 255, 255); ImageFill($thumb, 0, 0, $white); ImageColorTransparent($thumb, $white); } if (isset($_GET["square"])) { //正方形补白边 ImageCopyReSampled($thumb, $dest, $left, $top, 0, 0, $twidth, $theight, $nwidth, $nheight); } else { //长宽去白边 ImageCopyReSampled($thumb, $dest, $margin, $margin, 0, 0, $twidth, $theight, $nwidth, $nheight); } } ImageDestroy($dest); if (!$_GET["nomark"]) { if ($fwidth > 620 || $fheight > 620) { $logo = ImageCreateFrompng("./images/logo220.png"); $logosize = 220; } else { $logo = ImageCreateFrompng("./images/logo130.png"); $logosize = 130; } ImageSaveAlpha($logo, true); if (isset($_GET["square"])) { //正方形补白边 ImageCopy($thumb, $logo, $size - $logosize + $margin, $size - $logosize + $margin, 0, 0, $logosize, $logosize);