按比例调整图像大小

发布于 2024-11-17 00:49:14 字数 64 浏览 0 评论 0原文

我想将上传的图像调整为宽度:180px,高度成比例。有没有任何课程可以做到这一点?

感谢您的帮助!

i want to resize uploaded images to width: 180px with proportional height. Is there any classes to do this?

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

决绝 2024-11-24 00:49:14

我认为这个问题可以用实际的代码示例来回答。下面的代码向您展示了如何调整 uploaded 目录中图像的大小,并将调整后的图像保存在 resized 文件夹中。

<?php
// the file
$filename = 'uploaded/my_image.jpg';

// the desired width of the image
$width = 180;

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

list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;
$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, 'resized/my_image.jpg', 80);
?>

I think this question can use an answer with an actual code example. The code below shows you how you to resize an image inside a directory uploaded, and save the resized image in the folder resized.

<?php
// the file
$filename = 'uploaded/my_image.jpg';

// the desired width of the image
$width = 180;

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

list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;
$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, 'resized/my_image.jpg', 80);
?>
裸钻 2024-11-24 00:49:14

首先,您需要获取当前图像尺寸:

$width = imagesx($image);
$height = imagesy($image);

然后计算缩放因子:

$scalingFactor = $newImageWidth / $width;

当具有缩放因子时,只需计算图像的新高度:

$newImageHeight = $height * $scalingFactor;

然后创建新图像;

$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);

也许这些片段会有所帮助:

http:// /www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-php http://www.codeslices.net/snippets/resize-scale-image -proportionally-in-php

至少他们为我工作。

First you need to get the current image dimensions:

$width = imagesx($image);
$height = imagesy($image);

Then calculate the scaling factor:

$scalingFactor = $newImageWidth / $width;

When having the scaling factor just calculate the new height of the image:

$newImageHeight = $height * $scalingFactor;

Then just create the new image;

$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);

Probably these snippets will help:

http://www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-php http://www.codeslices.net/snippets/resize-scale-image-proportionally-in-php

at least they worked for me.

比忠 2024-11-24 00:49:14

您可以使用 imagecopyresampled php 函数。您还可以计算新的尺寸。

you may use imagecopyresampled php function. new sizes you also can calculate.

梦冥 2024-11-24 00:49:14

使用 jquery 插件 JCrop,并设置图像的长宽比...
检查此链接了解详细信息:
http://www.webresourcesdepot.com/jquery-image-crop-plugin- jcrop/

User jquery plugin JCrop, and set its aspect ratio for the image...
Check this link for details:
http://www.webresourcesdepot.com/jquery-image-crop-plugin-jcrop/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文