當用戶請求照片時,將圖像動態調整爲縮略圖的最佳方式是什麼?這些圖像已經以超過3MB的jpgs上傳了?我已經知道他們請求的圖像的數據庫中的文件名,例如:photo100.jpg,所以在php中創建縮略圖,然後將新創建的縮略圖t_photo100.jpg作爲img src =將是理想的在PHP中動態調整大圖像縮略圖?
回答
您可以使用類似PHPThumb這樣的庫來達到此目的。
它使用ImageMagick(如果可用)和PHP GD Library(附帶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" />
檢查demo page了更多的選擇。
希望這會有所幫助!
這是基於與TimThumb相同的代碼嗎?我曾經使用過TimThumb,但現在已經閱讀,由於以前的安全漏洞,它不受支持。這個PHPThumb是否繼承了這些? – Lee
@李:我還沒有使用PHPThumb,但我不認爲它從TimThumb繼承代碼。而不像另一個,這似乎有點維護 - https://github.com/JamesHeinrich/phpThumb –
在PHP中有功能來做到這一點。看看這個教程,看看如何調整圖像大小:http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
然後將所有的圖像請求導向到一個php文件(使用.htaccess),它可以找到請求的文件,通過調整器傳遞原始文件,然後輸出新的縮略圖
在我看來,最簡單的方法是使用ImageMagick的(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('@^.+(?<=/)(.+)[email protected]', '$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)
}
}
?>
Normaly你會使用這樣的:
<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文件,在創建完成後「保存」縮略圖,並在需要時簡單地返回該文件。
此外,這將繞過瀏覽器圖像緩存,導致每個頁面更多的流量刷新。
你可以看看這裏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.net:
2主要功能可能需要:
1:imagecopyresampled
<?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:imagecopyresized
<?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);
?>
- 1. 圖像縮略圖和調整大小
- 2. 調整圖像大小和縮略圖
- 3. 圖像處理 - 縮略圖自動調整大小/裁剪
- 4. twiiter bootstrap動態調整縮略圖
- 5. 動態調整縮略圖連續
- 6. RMagick:爲縮略圖縮放和調整圖像大小
- 7. JQuery調整縮略圖圖像
- 8. 從大圖像動態創建縮略圖預覽圖像
- 9. 試圖調整大小和裁剪圖像的縮略圖
- 10. 動態縮略圖用PHP
- 11. PHP圖像調整大小和動態圓角圖像角
- 12. 在Java中調整圖像大小以縮小圖像大小
- 13. PHP圖像調整大小 - 從中心平方/裁剪縮略圖?
- 14. 如何在PHP中動態調整鏈接圖像的大小?
- 15. 調整圖像大小並創建縮略圖
- 16. woocommerce使用全尺寸圖像,調整大小,縮略圖
- 17. 如何獲取調整大小/縮略圖圖像:UWP
- 18. 調整圖像大小或加載縮略圖
- 19. 將圖像調整爲縮略圖大小的問題
- 20. 調整相對於窗口大小的圖像縮略圖
- 21. PHP動態圖像調整大小(動態)
- 22. 照片庫縮略圖調整大小
- 23. PHP縮略圖圖像
- 24. 動態調整圖像大小。
- 25. CSS圖像動態調整大小
- 26. 動態圖像大小調整
- 27. JQuery:動態圖像調整大小
- 28. 動態圖像調整大小
- 29. 動態圖像調整大小系統
- 30. 動態圖像調整大小
我會建議你的在您的服務器上將縮略圖作爲單獨的圖像撕掉。您可以隨時通過html屬性或CSS設置圖片的寬度和高度,但這仍然會導致加載一個巨大的圖片。 當你上傳圖片到你的網站時,你可以使用upload.php類。文檔可以在這裏找到: http://www.verot.net/php_class_upload_docs.htm。 – james31rock