BLOB - PHP 调整图像大小

发布于 2024-11-09 03:58:56 字数 777 浏览 0 评论 0原文

我在调整 blob 图像大小时遇到​​一个小问题。 我发现我必须调整 BLOB 的高度和宽度,但是因为人们上传的图像不是方形的,我如何正确地重新调整它们的大小?

基本上我想要的最大宽度为 300px;

我当前的代码是

 $desired_width = 300;
 $desired_height = 300;

 $sth = mysql_query("SELECT photobase FROM userpictures WHERE id = '".$array[0]."'");

 while($r = mysql_fetch_assoc($sth)) {
      $blobcontents = $r["photobase"];

      $im = imagecreatefromstring($blobcontents);
      $new = imagecreatetruecolor($desired_width, $desired_height);

      $x = imagesx($im);
      $y = imagesy($im);

      imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);

      imagedestroy($im);

      header('Content-type: <span class="posthilit">image</span>/jpeg');

      imagejpeg($new, null, 85);

I am having a small issue re-sizing blob images.
What I have found is I have to do height and width of the BLOB but because people upload images that are not square, how do I re-size them correctly?

Basicly I want a max width of 300px;

my current code is

 $desired_width = 300;
 $desired_height = 300;

 $sth = mysql_query("SELECT photobase FROM userpictures WHERE id = '".$array[0]."'");

 while($r = mysql_fetch_assoc($sth)) {
      $blobcontents = $r["photobase"];

      $im = imagecreatefromstring($blobcontents);
      $new = imagecreatetruecolor($desired_width, $desired_height);

      $x = imagesx($im);
      $y = imagesy($im);

      imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);

      imagedestroy($im);

      header('Content-type: <span class="posthilit">image</span>/jpeg');

      imagejpeg($new, null, 85);

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

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

发布评论

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

评论(1

陪你搞怪i 2024-11-16 03:58:56

调整图像大小并保持约束比例的简单方法:

<?php
// Constraints
$max_width = 100;
$max_height = 100;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$width);
$height = intval($ratio*$height);
?>

A simple method to resize a image keeping the constraint proportions:

<?php
// Constraints
$max_width = 100;
$max_height = 100;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$width);
$height = intval($ratio*$height);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文