我正在创建一个应用程序,它需要通过php脚本将大图像转换为缩略图,然后将其编码为base64,以便我可以通过json将它发送到我的android应用程序。 我有调整图像大小的问题。我需要PHP脚本,帮助我做到这一点如何通过php脚本调整图像大小
回答
你可以试试这个图像调整大小功能tutorial
而且你也可以使用这个代码调整大小函数(GD)。
<?php
$thumb = new Imagick();
$thumb->readImage('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy();
?>
Or, a shorter version of the same:
<?php
$thumb = new Imagick('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->destroy();
?>
并且还指此链接用于调整尺寸图像
2. 9Lession
而且也变换为Base64编码图像下面参考本Links
您也可以使用[$ this-> cropThumbnailImage](http://php.net/manual/en/imagick.cropthumbnailimage.php)代替'resizeImage'来避免拉伸缩略图。 – Flimm
你可以尝试imagick - http://php.net/manual/en/imagick.resizeimage.php调整图像大小。示例代码:如果您使用的GD
<?php
$thumb = new Imagick();
$thumb->readImage('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy();
?>
代码会
<?php
// File and new size
$filename = 'test.jpg';
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = YOUR REQUIRED WIDTH;
$newheight = YOUR REQUIRED HEIGHT;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
可以使用例如TimThumb
timthumb.php?src=img.jpg&h=height&w=width
那么你应该编码图像: 要创建缩略图到base64: How to convert an image to base64 encoding?
的代码创建一个名为createThumbs的函数,该函数将获得th ree参数。第一个和第二个相应地是包含原始图像的目录的路径以及放置缩略图的目录的路径。第三个参数是您想要的缩略图图像的宽度。
<?php
function createThumbs($pathToImages, $pathToThumbs, $thumbWidth)
{
// open the directory
$dir = opendir($pathToImages);
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir($dir))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if (strtolower($info['extension']) == 'jpg')
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg("{$pathToImages}{$fname}");
$width = imagesx($img);
$height = imagesy($img);
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor($height * ($thumbWidth/$width));
// create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// save thumbnail into a file
imagejpeg($tmp_img, "{$pathToThumbs}{$fname}");
}
}
// close the directory
closedir($dir);
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>
+1为好功能 –
的职位是有点老了,但如果有人想,我刚才提出这个功能
function save_image_from_64($path, $name, $dimension, $original)
{
header("Content-Type: image/jpeg");
list($width,$height) = explode('x',$dimension); //Getting new height and width
$img = str_replace('data:image/jpeg;base64,', '', $original); //Getting the base64 image
$image = imagecreatefromstring(base64_decode($img));
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
imagejpeg($new_image, $path.$name.'.jpg'); // saving the image
}
save_image_from_64("cdn/","test","800x200","data:image/jpeg;base64,/9j/4AAQS...");
- 1. php调整大小图像脚本
- 2. PHP图像调整大小脚本?
- 3. 无法通过动作脚本正确调整图像大小
- 4. 使用imagemagick通过shell脚本调整图像大小
- 5. 如何在此PHP脚本中调整图像大小?
- 6. 谷歌脚本图像调整大小
- 7. 通过javascript调整图像大小或调整图像尺寸
- 8. 调整图像大小如果它大于X通过批处理脚本
- 9. 通过鼠标调整图像大小
- 10. php调整大小图像
- 11. PHP:调整图像大小
- 12. PHP图像调整大小
- 13. 如何通过调整大小使用Jcrop裁剪大图像?
- 14. 需要一个PHP图像调整大小/裁剪脚本
- 15. 使用php脚本调整图像大小
- 16. 提高PHP图像上传/调整大小脚本的性能
- 17. 上传uploadify通过php调整图像大小
- 18. PHP图像调整大小脚本从iPhone摄像头上传旋转图像
- 19. SDWebImage:UIImageView通过下载图像的大小来调整大小
- 20. 如何通过window.resize函数调整图像大小?
- 21. 用Java调整图像大小。如何调整大小
- 22. 如何在调整窗口大小时调整图像大小?
- 23. 如何根据图像大小调整图像视图大小?
- 24. Php调整大小与图像GD不显示图像,但调整大小
- 25. PHP图像调整大小与存储调整大小的图像
- 26. PHP高级图像调整大小,类似于iOS调整图像的大小
- 27. 模态图像中的箭头(通过图像调整大小)
- 28. 如何使用PHP调整图像大小和裁剪图像?
- 29. 如何在调整图像大小时调整图像大小使用drawImage
- 30. 通过调整图像url来裁剪图像以调整大小
什么是您所使用的图像处理库? – San