当用户请求照片时,将图像动态调整为缩略图的最佳方法是什么?这些图片已经上传为高分辨率超过3MB的jpg了吗?我已经从他们请求的图像的数据库中知道了文件名,例如:photo100.jpg,所以在php中创建一个缩略图,然后将新创建的缩略图t_photo100.jpg作为img src=将是理想的
您可以在这里查看SimpleImage类:
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
我用它,它很好,很简单。基本上你可以只做
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');
您可以使用此预处理一次并显示新创建的图像。
php中有一些函数可以做到这一点。查看本教程,了解如何调整图像大小:http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
然后将所有图像请求定向到一个php文件(使用.htaccess),该文件可以找到请求的文件,通过resizer传递原始文件,然后输出新的缩略图
您可以使用类似PHPThumb的库来实现此目的。
它使用ImageMagick(如果可用)和PHP GD库(随PHP v4.3.0+提供)来处理图像。
一旦配置和设置好,你就可以动态生成图像缩略图,比如:
<img src="/uploads/phpThumb.php?src=images/logo.png&w=100" />
<img src="/uploads/phpThumb.php?src=images/foobar.png&h=50&w=50&zc=1" />
查看演示页面了解更多选项。
希望这能有所帮助!
在我看来,最简单的方法是使用imagemagik(http://phpsnips.com/snip-111#.UhT5XZJwok0):
<?php
// Location to upload main image:
$mainDir = $_SERVER['DOCUMENT_ROOT'].'/images/l/';
// Location to create the thumb image:
$smalDir = $_SERVER['DOCUMENT_ROOT'].'/images/s/';
// Command to use:
$command = '/usr/bin/convert';
// Thumbnail width:
$size = 210;
// Make sure we have an image:
if(isset($_POST['submit'])){
if(getimagesize($_FILES['photo']['tmp_name'])){
$name = $_FILES['photo']['name'];
$uploadfile = $mainDir . $name;
move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
$lrgImg = $mainDir . $name;
$smlImg = $smalDir . $name;
$imageMagick = $command . " '". $lrgImg . "' -resize '$size' '" . $smlImg . "'";
shell_exec($imageMagick);
}
header("Location: /test.php");
exit;
}else{
?>
<form action=" <?php echo $_SERVER['PHP_SELF']; ?> " method="post" enctype="multipart/form-data">
<p><input type="file" name="photo" /></p>
<p><input type="submit" value="Upload!" name="submit" /></p>
</form>
<?php
foreach(glob($smalDir.'*') as $img){
echo ' <img src="'.str_replace($_SERVER['DOCUMENT_ROOT'], '',$img).'" /> ';
}
}
?>
你也可以使用PHPGD(http://phpsnips.com/snip-5#.UhT5yJJwok0):
<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
$details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,
$details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.
(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcImg);
imagedestroy($thumbImg);
}
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);
//120 = thumb width :: 80 = thumb quality (1-100)
}
}
?>
通常你会使用这样的东西:
<img src="/images/uploads/image50.jpg" />
在生成html时,将其替换为类似的内容。
<img src="image.php?img_url=<?=base_64_encode("/images/uploads/image50.jpg")?>&width=128" />
然后使用类似这样的image.php文件:
<?php
header("Content-Type: image/jpeg");
$imgUrl = base64_decode($_GET["img_url"]);
$c = file_get_contents($imgUrl);
$arr = getimagesizefromstring($c);
$img = imagecreatefromstring($c);
if (!is_array($arr)) {
//remote image is not available. Use default one.
$c = file_get_contents("include/images/nobanner.jpg");
}
if (isset($_GET["width"])){
//Get Width and Height
List($Width, $Height) = getimagesize($imgUrl);
//Calc new Size
$w = $_GET["width"];
$h = $Height * ($w / $Width);
//Build the image
//Create new Base
$NewImageBase = imagecreatetruecolor($w, $h);
//copy image
imagecopyresampled($NewImageBase, $img, 0, 0, 0, 0, $w, $h, $Width, $Height);
$img = $NewImageBase;
}
imagejpeg($img);
?>
但是请记住,每次有人访问图像时,这都会导致密集的计算。您可以优化image.php文件,以便在创建缩略图后"保存"缩略图,并在需要时简单地返回该文件。
此外,这将绕过浏览器的图像缓存,导致每次刷新页面时流量增加。
来自Php.net:
2您可能需要的主要功能:
1:图像复制重新采样
<?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>
2:图像复制调整大小
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
我也面临类似的问题。我搜索了一下,但找不到合适的解决方案。所以,我创造了一个。这将与CDN(cloudfront)一起降低php Web服务器上的负载。我已经在生产中使用了不同尺寸的设备,结果令人震惊。请尝试一下:https://github.com/thekosmix/php-image-resizer
工作原理:使用http://url/width/height/imagename.如果它存在于CDN中,它将得到服务。否则,它将访问php服务器,调整原始图像的大小并将其存储在CDN上,然后开始为每个请求提供服务。