2010-07-15 68 views
11

即时通讯正在寻找创建尺寸为100px乘100px的缩略图。我已经看过很多文章解释方法,但如果要保持尺寸比例,最终会得到宽度!=高度。PHP裁剪图像,以固定宽度和高度,而不会损失尺寸比例

例如,我有一个450 x 350像素的图像。我想裁剪为100px×100px。如果我保持这个比例,我最终会有100px乘77px。当我将这些图像列在行和列中时,这使得它变得丑陋。然而,没有尺寸比的图像看起来也很糟糕。

我已经看到flickr的图像,它们看起来很棒。例如:
缩略图:http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
介质尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
大尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

TKS

+0

ohh现在我知道为什么有人责备我为那个b4。我必须点击勾号。 omg我没有注意到它。好吧,让我看看回复 – nuttynibbles 2010-07-15 14:53:57

回答

34

这是通过仅使用图像作为具有1缩略图的一部分进行:1的纵横比率(主要是图像的中心)。如果仔细观察,您可以在flickr缩略图中看到它。

因为您的问题中有“crop”,我不确定您是否已经知道这一点,但是您想知道什么?

要使用裁剪,这里有一个例子:

//Your Image 
$imgSrc = "image.jpg"; 

//getting the image dimensions 
list($width, $height) = getimagesize($imgSrc); 

//saving the image into memory (for manipulation with GD Library) 
$myImage = imagecreatefromjpeg($imgSrc); 

// calculating the part of the image to use for thumbnail 
if ($width > $height) { 
    $y = 0; 
    $x = ($width - $height)/2; 
    $smallestSide = $height; 
} else { 
    $x = 0; 
    $y = ($height - $width)/2; 
    $smallestSide = $width; 
} 

// copying the part into thumbnail 
$thumbSize = 100; 
$thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

//final output 
header('Content-type: image/jpeg'); 
imagejpeg($thumb); 
+0

嗨skoschnike *仔细检查,如果我拼写你的名字正确=)* 已经跨过我的脑海。但即时通讯如何做到这一点。关心分享? – nuttynibbles 2010-07-15 14:57:03

+0

嘿斯文,我试过你的脚本,它的作品很棒。非常感谢你。无瑕 – nuttynibbles 2010-07-16 17:16:39

+0

嘿斯文,我只是有一个问题。使用你的脚本很好。但我意识到裁剪后的图像质量与原始图像不一样。举例来说,我从flickr拍摄了一张样片并裁剪到相同的尺寸,并且它们看起来不同。任何解决方案保留质量? – nuttynibbles 2010-07-23 08:52:24

0

我喜欢用GDLib与图像不甘示弱,它飞驰容易也上班。那里有很多博客文章和教程。我建议使用类或类似的类,因为照顾图像中的所有变化可能非常耗时。基于较小的宽度或高度

public function croppThis($target_url) { 

    $this->jpegImgCrop($target_url); 

} 

$ target_url具有正方形

+0

也许这去吧? http://pear.php.net/package/Image_Transform – 2010-07-15 14:57:29

3

裁剪图像 - 图像是名称。

public function jpegImgCrop($target_url) {//support 



    $image = imagecreatefromjpeg($target_url); 
    $filename = $target_url; 
    $width = imagesx($image); 
    $height = imagesy($image); 
    $image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM 

    if($width==$height) { 

    $thumb_width = $width; 
    $thumb_height = $height; 

    } elseif($width<$height) { 

    $thumb_width = $width; 
    $thumb_height = $width; 

    } elseif($width>$height) { 

    $thumb_width = $height; 
    $thumb_height = $height; 

    } else { 
    $thumb_width = 150; 
    $thumb_height = 150; 
    } 

    $original_aspect = $width/$height; 
    $thumb_aspect = $thumb_width/$thumb_height; 

    if ($original_aspect >= $thumb_aspect) { 

    // If image is wider than thumbnail (in aspect ratio sense) 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 

    } 
    else { 
    // If the thumbnail is wider than the image 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
    } 

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 

    // Resize and crop 
    imagecopyresampled($thumb, 
     $image, 
     0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
     0 - ($new_height - $thumb_height)/2, // Center the image vertically 
     0, 0, 
     $new_width, $new_height, 
     $width, $height); 
    imagejpeg($thumb, $filename, 80); 

} 
+0

简单的工作解决方案;但首先检查文件类型,并使用JPG,PNG或GIF的imagecreate可能会更有帮助。 – web2kx 2014-04-23 03:38:25

+0

如何工作这.. plz帮助我..如何显示像图像 – user3501407 2014-07-07 11:11:28

3

您可以使用此代码。 您需要传递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); 
    } 
    } 
} 
+1

它创建拇指在哪里?什么是拇指路径?写一些描述。 – 2017-10-18 05:59:34

+1

@PathikVejani谢谢你,我添加了一些说明。 – 2017-10-18 15:39:00

相关问题