Q
PHP臨時縮略圖
0
A
回答
1
您可以使用這樣的功能。
參考http://tutorialfeed.net/development/scale-an-image-using-php
function create_thumb($imgSrc, $thumbnail_width, $thumbnail_height, $dest_src, $ext)
{
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
// Check if the images is a gif
if($ext == 'gif')
{
$myImage = imagecreatefromgif($imgSrc);
}
// Check if the image is a png
elseif($ext == 'png')
{
$myImage = imagecreatefrompng($imgSrc);
}
// Otherwise, file is jpeg
else
{
$myImage = imagecreatefromjpeg($imgSrc);
}
// Find the original ratio
$ratio_orig = $width_orig/$height_orig;
// Check whether to scale initially by height or by width
if($thumbnail_width/$thumbnail_height > $ratio_orig)
{
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
}
else
{
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
// Scale the image down and the reduce the other axis to create the thumbnail
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
// Depending on the file extension, save the file
if($ext == 'gif')
{
imagegif($thumb, $dest_src);
}
elseif($ext == 'png')
{
imagepng($thumb, $dest_src);
}
else
{
imagejpeg($thumb, $dest_src, 100);
}
// Remove rubbish file data
imagedestroy($process);
imagedestroy($myImage);
// Return thumb (success/fail)
return $thumb;
}
+0
我最終使用了它,因爲它適用於所有文件類型,謝謝 – user1083382 2012-02-17 11:27:05
0
可以使用ImageWorkshop的resizeInPourcent()方法(使用庫GD庫):http://phpimageworkshop.com/doc/17/resizing.html
例:
<?php
$myImage->resizeInPourcent(50, 50); // Resize to get 50% width and height
相關問題
- 1. 在即時縮略圖PHP
- 2. PHP縮略圖
- 3. PHP縮略圖圖像
- 4. 與PHP實時jQuery縮略圖
- 5. 分頁縮略圖PHP
- 6. 動態縮略圖用PHP
- 7. php縮略圖錯誤
- 8. PHP/Ajax分頁縮略圖
- 9. PHP縮略圖生成
- 10. 使用PHP創建縮略圖或手動添加縮略圖?
- 11. PHP縮略圖生成器未處理縮略圖
- 12. ng-file-upload v12.0.1 - 生成縮略圖時顯示臨時的「加載」圖標
- 13. 即時創建縮略圖
- 14. 縮略圖生成時間
- 15. YouTube v3中的縮略圖縮略圖
- 16. 生成縮略圖與易縮略圖
- 17. PHP圖像縮略圖性能?
- 18. PHP,操縱圖像和縮略圖?
- 19. PHP圖片上傳壓縮臨時文件大小
- 20. DropBox縮略圖
- 21. 縮略圖
- 22. JavaScript,縮略圖()
- 23. FLV縮略圖
- 24. jquerymobile縮略圖
- 25. Evernote縮略圖
- 26. Django縮略圖
- 27. BxSlider縮略圖
- 28. imagemagick縮略圖
- 29. 縮略圖asp.net
- 30. 縮略圖
imagecopyresampled:HTTP:/ /php.net/manual/en/function.imagecopyresampled.php – dbrumann 2012-02-17 10:33:52
查看PHP GD庫。 – deed02392 2012-02-17 10:33:59
@mahok完美謝謝你! – user1083382 2012-02-17 11:02:00