php 上传后php简单图像调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8030200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:48:53  来源:igfitidea点击:

php simple image resize after upload

phpimagejoomlaresize

提问by Mankind1023

I looked around for a while and found some very confusing and complicated stuff that I couldn't get to work.

我环顾四周,发现了一些我无法开始工作的非常混乱和复杂的东西。

I'm using chronoforms with joomla to make a form with an upload file script, and it uploads the image to the server with no problems, so far so good.

我正在使用带有 joomla 的 chronoforms 来制作带有上传文件脚本的表单,并将图像上传到服务器没有任何问题,到目前为止一切顺利。

I need to take the uploaded image and resize it, better yet, is there a way to resize the image before uploading it to the server?

我需要获取上传的图像并调整其大小,更好的是,有没有办法在将图像上传到服务器之前调整其大小?

Thanks.

谢谢。

回答by Nimrod007

I use this easy 1 function that does it all

我使用这个简单的 1 函数来完成这一切

check it out :

一探究竟 :

http://www.nimrodstech.com/php-image-resize/

http://www.nimrodstech.com/php-image-resize/

https://github.com/Nimrod007/PHP_image_resize

https://github.com/Nimrod007/PHP_image_resize

回答by Mike Purcell

I have used PHPThumbfor a few of my projects and found it easy to work with and has a small resource footprint. You can read the docs for more info, but is pretty easy:

我已经将PHPThumb用于我的一些项目,发现它易于使用并且占用资源很少。您可以阅读文档以获取更多信息,但非常简单:

$thumb = PhpThumbFactory::create('/path/to/source/image.png');

$thumb->resize(100, 100);

$thumb->save('/path/where/you/want/resized/image.png');

回答by Herr

[This example] is what you are looking for imagecopyresampled.

[此示例] 是您要查找的imagecopyresampled

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

回答by Sz.

Chronoforms (v4 here) does support this out of the box! (I've seen random traces of this for older versions, too, down to 1.3.)

Chronoforms(此处为 v4)确实支持开箱即用!(我也看到了旧版本的随机痕迹,低至 1.3。)

I can just drag an Image Resizeaction (from under Utilites) to a desired form event (under On Submit).

我可以将一个Image Resize动作(从下Utilites)拖到所需的表单事件(下On Submit)。

NOTE: this is notfor client-side resizing. For that you'd need a Javascript form uploader package, which can show a thumbnail before & during the upload. They are usually non-trivial to integrate. (And using those client-side thumbnails also for uploadingalong with the original image requires some even more advanced -- and accordingly more complicated -- stuff; I'd say it's rarely worth the extra pain, just bite the bullet and generate the thumbnail again on the server, and think about all those poor African kids, who have even tougher lifes than web developers. ;) )

注意:这不适用于客户端调整大小。为此,您需要一个 Javascript 表单上传程序包,它可以在上传之前和期间显示缩略图。它们通常很难集成。(并且使用这些客户端缩略图与原始图像一起上传需要一些更高级 - 因此更复杂 - 的东西;我想说这很少值得额外的痛苦,只是硬着头皮生成缩略图再次在服务器上,想想那些比 Web 开发人员更艰难的生活的非洲孩子。;) )

回答by Jarrod

Here is a simple resize library I created and can be found on here on Github.

这是我创建的一个简单的调整大小库,可以在 Github 上找到。

Will work with any framework.

将适用于任何框架。

An example of how to use the library:

如何使用库的示例:

// Include PHP Image Magician library
require_once('php_image_magician.php');

// Open JPG image
$magicianObj = new imageLib('racecar.jpg');

// Resize to best fit then crop (check out the other options)
$magicianObj -> resizeImage(100, 200, 'crop');

// Save resized image as a PNG (or jpg, bmp, etc)
$magicianObj -> saveImage('racecar_small.png');