使用 PHP 建议调整图像大小

发布于 2024-09-10 16:54:51 字数 1271 浏览 4 评论 0原文

根据以前的经验,我注意到到目前为止,我不太擅长将其他人创建的“脚本”与我自己的现有代码集成。我需要一些建议来理解这个人所说的关于使用 PHP 调整图像大小的内容:

在评论中,他写道:

// Parameters need to be passed in through the URL's query string:
// image        absolute path of local image starting with "/" (e.g. /images/toast.jpg)
// width        maximum width of final image in pixels (e.g. 700)
// height       maximum height of final image in pixels (e.g. 700)

然后,他给出了一个实际的例子,也作为评论:

// Resizing and cropping a JPEG into a square:
// <img src="/image.php/image-name.jpg?width=100&amp;height=100&amp;cropratio=1:1&amp;image=/path/to/image.jpg" alt="Don't forget your alt text" />

我猜,他希望脚本的用户类似于上面的img src。所以,我的问题是:我如何真正使我的图片来源与上面的示例类似?下面是我的一些代码。它显示了我如何保存图片以及如何回显/显示它们。它来自 uploader.php

move_uploaded_file($_FILES["file"]["tmp_name"],
      "profileportraits/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "profileportraits/" . $_FILES["file"]["name"]; 

将照片保存到文件夹中后,我将文件路径保存在 MySQL 表中,然后调用文件路径来显示图片(下面的代码)。它来自 profile.php

echo "<img src=\"{$row['PortraitPath']}\" />";

因此,如果我使用上面的 img src 来显示“实际”图片,我将如何传递类似于“脚本示例”中的参数?

谢谢。

From previous experiences, I've noticed that I'm not very good at integrating 'scripts' created by others, with my own existing code, as of now. I need some advice on understanding what this person is saying about resizing images with PHP:

In the comments, he's written:

// Parameters need to be passed in through the URL's query string:
// image        absolute path of local image starting with "/" (e.g. /images/toast.jpg)
// width        maximum width of final image in pixels (e.g. 700)
// height       maximum height of final image in pixels (e.g. 700)

Then, he gives an actual example, also as a comment:

// Resizing and cropping a JPEG into a square:
// <img src="/image.php/image-name.jpg?width=100&height=100&cropratio=1:1&image=/path/to/image.jpg" alt="Don't forget your alt text" />

I am guessing, he wants the user of the script to have something similar to the above img src. So, my question is: How will I actually make the source of my pictures similar to the example above? Below is some of the code I have. It shows how I am saving the pictures and HOW I am echoing/displaying them. It's from uploader.php:

move_uploaded_file($_FILES["file"]["tmp_name"],
      "profileportraits/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "profileportraits/" . $_FILES["file"]["name"]; 

Once the photo has been saved in the folder, I save the file path in a MySQL table and later call the file path to display the picture (Below Code). It's from profile.php

echo "<img src=\"{$row['PortraitPath']}\" />";

Therefore, how will I pass the parameters similar to the one in the 'script example' if I am using the above img src to display the 'actual' picture?

THANK YOU.

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

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

发布评论

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

评论(1

羁拥 2024-09-17 16:54:51
$width = 100;
$height = 100;

src="/image.php?width={$width}&height={$height}"

您可以使用以下代码来调整图像大小...一旦获得所有参数

/**
 * Gets the jpeg contents of the resized version of an already uploaded image
 * (Returns false if the file was not an image)
 *
 * @param string $input_name The name of the file on the disk
 * @param int $maxwidth The desired width of the resized image
 * @param int $maxheight The desired height of the resized image
 * @param true|false $square If set to true, takes the smallest of maxwidth and
 *          maxheight and use it to set the dimensions on the new image. If no
 *          crop parameters are set, the largest square that fits in the image
 *          centered will be used for the resize. If square, the crop must be a
 *          square region.
 * @param int $x1 x coordinate for top, left corner
 * @param int $y1 y coordinate for top, left corner
 * @param int $x2 x coordinate for bottom, right corner
 * @param int $y2 y coordinate for bottom, right corner
 * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
 * @return false|mixed The contents of the resized image, or false on failure
 */
function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) {
    // Get the size information from the image
    $imgsizearray = getimagesize($input_name);
    if ($imgsizearray == FALSE) {
        return FALSE;
    }

    // Get width and height
    $width = $imgsizearray[0];
    $height = $imgsizearray[1];

    // make sure we can read the image
    $accepted_formats = array(
        'image/jpeg' => 'jpeg',
        'image/pjpeg' => 'jpeg',
        'image/png' => 'png',
        'image/x-png' => 'png',
        'image/gif' => 'gif'
    );

    // make sure the function is available
    $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
    if (!is_callable($load_function)) {
        return FALSE;
    }

    // crop image first?
    $crop = TRUE;
    if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
        $crop = FALSE;
    }

    // how large a section of the image has been selected
    if ($crop) {
        $region_width = $x2 - $x1;
        $region_height = $y2 - $y1;
    } else {
        // everything selected if no crop parameters
        $region_width = $width;
        $region_height = $height;
    }

    // determine cropping offsets
    if ($square) {
        // asking for a square image back

        // detect case where someone is passing crop parameters that are not for a square
        if ($crop == TRUE && $region_width != $region_height) {
            return FALSE;
        }

        // size of the new square image
        $new_width = $new_height = min($maxwidth, $maxheight);

        // find largest square that fits within the selected region
        $region_width = $region_height = min($region_width, $region_height);

        // set offsets for crop
        if ($crop) {
            $widthoffset = $x1;
            $heightoffset = $y1;
            $width = $x2 - $x1;
            $height = $width;
        } else {
            // place square region in the center
            $widthoffset = floor(($width - $region_width) / 2);
            $heightoffset = floor(($height - $region_height) / 2);
        }
    } else {
        // non-square new image

        $new_width = $maxwidth;
        $new_height = $maxwidth;

        // maintain aspect ratio of original image/crop
        if (($region_height / (float)$new_height) > ($region_width / (float)$new_width)) {
            $new_width = floor($new_height * $region_width / (float)$region_height);
        } else {
            $new_height = floor($new_width * $region_height / (float)$region_width);
        }

        // by default, use entire image
        $widthoffset = 0;
        $heightoffset = 0;

        if ($crop) {
            $widthoffset = $x1;
            $heightoffset = $y1;
        }
    }

    // check for upscaling
    // @todo This ignores squares, coordinates, and cropping. It's probably not the best idea.
    // Size checking should be done in action code, but for backward compatibility
    // this duplicates the previous behavior.
    if (!$upscale && ($height < $new_height || $width < $new_width)) {
        // zero out offsets
        $widthoffset = $heightoffset = 0;

        // determine if we can scale it down at all
        // (ie, if only one dimension is too small)
        // if not, just use original size.
        if ($height < $new_height && $width < $new_width) {
            $ratio = 1;
        } elseif ($height < $new_height) {
            $ratio = $new_width / $width;
        } elseif ($width < $new_width) {
            $ratio = $new_height / $height;
        }
        $region_height = $height;
        $region_width = $width;
        $new_height = floor($height * $ratio);
        $new_width = floor($width * $ratio);
    }

    // load original image
    $orig_image = $load_function($input_name);
    if (!$orig_image) {
        return FALSE;
    }

    // allocate the new image
    $newimage = imagecreatetruecolor($new_width, $new_height);
    if (!$newimage) {
        return FALSE;
    }

    // create the new image
    $rtn_code = imagecopyresampled( $newimage,
                                    $orig_image,
                                    0,
                                    0,
                                    $widthoffset,
                                    $heightoffset,
                                    $new_width,
                                    $new_height,
                                    $region_width,
                                    $region_height );
    if (!$rtn_code) {
        return FALSE;
    }

    // grab contents for return
    ob_start();
    imagejpeg($newimage, null, 90);
    $jpeg = ob_get_clean();

    imagedestroy($newimage);
    imagedestroy($orig_image);

    return $jpeg;
}

希望这有帮助..

谢谢
切坦夏尔马

$width = 100;
$height = 100;

src="/image.php?width={$width}&height={$height}"

You can use the following code to resize the image... once you got all the params

/**
 * Gets the jpeg contents of the resized version of an already uploaded image
 * (Returns false if the file was not an image)
 *
 * @param string $input_name The name of the file on the disk
 * @param int $maxwidth The desired width of the resized image
 * @param int $maxheight The desired height of the resized image
 * @param true|false $square If set to true, takes the smallest of maxwidth and
 *          maxheight and use it to set the dimensions on the new image. If no
 *          crop parameters are set, the largest square that fits in the image
 *          centered will be used for the resize. If square, the crop must be a
 *          square region.
 * @param int $x1 x coordinate for top, left corner
 * @param int $y1 y coordinate for top, left corner
 * @param int $x2 x coordinate for bottom, right corner
 * @param int $y2 y coordinate for bottom, right corner
 * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
 * @return false|mixed The contents of the resized image, or false on failure
 */
function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) {
    // Get the size information from the image
    $imgsizearray = getimagesize($input_name);
    if ($imgsizearray == FALSE) {
        return FALSE;
    }

    // Get width and height
    $width = $imgsizearray[0];
    $height = $imgsizearray[1];

    // make sure we can read the image
    $accepted_formats = array(
        'image/jpeg' => 'jpeg',
        'image/pjpeg' => 'jpeg',
        'image/png' => 'png',
        'image/x-png' => 'png',
        'image/gif' => 'gif'
    );

    // make sure the function is available
    $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
    if (!is_callable($load_function)) {
        return FALSE;
    }

    // crop image first?
    $crop = TRUE;
    if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
        $crop = FALSE;
    }

    // how large a section of the image has been selected
    if ($crop) {
        $region_width = $x2 - $x1;
        $region_height = $y2 - $y1;
    } else {
        // everything selected if no crop parameters
        $region_width = $width;
        $region_height = $height;
    }

    // determine cropping offsets
    if ($square) {
        // asking for a square image back

        // detect case where someone is passing crop parameters that are not for a square
        if ($crop == TRUE && $region_width != $region_height) {
            return FALSE;
        }

        // size of the new square image
        $new_width = $new_height = min($maxwidth, $maxheight);

        // find largest square that fits within the selected region
        $region_width = $region_height = min($region_width, $region_height);

        // set offsets for crop
        if ($crop) {
            $widthoffset = $x1;
            $heightoffset = $y1;
            $width = $x2 - $x1;
            $height = $width;
        } else {
            // place square region in the center
            $widthoffset = floor(($width - $region_width) / 2);
            $heightoffset = floor(($height - $region_height) / 2);
        }
    } else {
        // non-square new image

        $new_width = $maxwidth;
        $new_height = $maxwidth;

        // maintain aspect ratio of original image/crop
        if (($region_height / (float)$new_height) > ($region_width / (float)$new_width)) {
            $new_width = floor($new_height * $region_width / (float)$region_height);
        } else {
            $new_height = floor($new_width * $region_height / (float)$region_width);
        }

        // by default, use entire image
        $widthoffset = 0;
        $heightoffset = 0;

        if ($crop) {
            $widthoffset = $x1;
            $heightoffset = $y1;
        }
    }

    // check for upscaling
    // @todo This ignores squares, coordinates, and cropping. It's probably not the best idea.
    // Size checking should be done in action code, but for backward compatibility
    // this duplicates the previous behavior.
    if (!$upscale && ($height < $new_height || $width < $new_width)) {
        // zero out offsets
        $widthoffset = $heightoffset = 0;

        // determine if we can scale it down at all
        // (ie, if only one dimension is too small)
        // if not, just use original size.
        if ($height < $new_height && $width < $new_width) {
            $ratio = 1;
        } elseif ($height < $new_height) {
            $ratio = $new_width / $width;
        } elseif ($width < $new_width) {
            $ratio = $new_height / $height;
        }
        $region_height = $height;
        $region_width = $width;
        $new_height = floor($height * $ratio);
        $new_width = floor($width * $ratio);
    }

    // load original image
    $orig_image = $load_function($input_name);
    if (!$orig_image) {
        return FALSE;
    }

    // allocate the new image
    $newimage = imagecreatetruecolor($new_width, $new_height);
    if (!$newimage) {
        return FALSE;
    }

    // create the new image
    $rtn_code = imagecopyresampled( $newimage,
                                    $orig_image,
                                    0,
                                    0,
                                    $widthoffset,
                                    $heightoffset,
                                    $new_width,
                                    $new_height,
                                    $region_width,
                                    $region_height );
    if (!$rtn_code) {
        return FALSE;
    }

    // grab contents for return
    ob_start();
    imagejpeg($newimage, null, 90);
    $jpeg = ob_get_clean();

    imagedestroy($newimage);
    imagedestroy($orig_image);

    return $jpeg;
}

Hope this helps..

Thanks
Chetan sharma

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