您可以使用此代码。 您需要传递px中的源图像路径和缩略图大小以及可选的目标路径。如果你通过它将保存图像,否则拇指将被显示。
只允许使用jpg,jpeg和png。
function cropImage($sourcePath, $thumbSize, $destination = null) {
$parts = explode('.', $sourcePath);
$ext = $parts[count($parts) - 1];
if ($ext == 'jpg' || $ext == 'jpeg') {
$format = 'jpg';
} else {
$format = 'png';
}
if ($format == 'jpg') {
$sourceImage = imagecreatefromjpeg($sourcePath);
}
if ($format == 'png') {
$sourceImage = imagecreatefrompng($sourcePath);
}
list($srcWidth, $srcHeight) = getimagesize($sourcePath);
// calculating the part of the image to use for thumbnail
if ($srcWidth > $srcHeight) {
$y = 0;
$x = ($srcWidth - $srcHeight)/2;
$smallestSide = $srcHeight;
} else {
$x = 0;
$y = ($srcHeight - $srcWidth)/2;
$smallestSide = $srcWidth;
}
$destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
if ($destination == null) {
header('Content-Type: image/jpeg');
if ($format == 'jpg') {
imagejpeg($destinationImage, null, 100);
}
if ($format == 'png') {
imagejpeg($destinationImage);
}
if ($destination = null) {
}
} else {
if ($format == 'jpg') {
imagejpeg($destinationImage, $destination, 100);
}
if ($format == 'png') {
imagepng($destinationImage, $destination);
}
}
}
ohh现在我知道为什么有人责备我为那个b4。我必须点击勾号。 omg我没有注意到它。好吧,让我看看回复 – nuttynibbles 2010-07-15 14:53:57