php - 使用 php 直接从 url 调整图像大小

标签 php image-processing

我不确定这是否可以完成,所以在这里发布。我有一个图像存储在网络服务器中。例如 http://www.abc.com/myimage.png

现在我想调整该图像的大小,但不在我的本地中。因此它将是直接从 url 调整图像大小。

请建议是否有任何指南或示例。

最佳答案

如果你不想使用GD库,可以使用这个函数。

警告

Resizing images on the fly can really slow down the page

代码:

function getImageXY( $image, $imgDimsMax=140 ) {
    $top = 0;
    $left = 0;

    $aspectRatio= 1;    // deafult aspect ratio...keep the image as is.

    // set the default dimensions.
    $imgWidth   = $imgDimsMax;
    $imgHeight  = $imgDimsMax;

    list( $width, $height, $type, $attr ) = getimagesize( $image );

    if( $width == $height ) {
        // if the image is less than ( $imgDimsMax x $imgDimsMax ), use it as is..
        if( $width < $imgDimsMax ) {
            $imgWidth   = $width;
            $imgHeight  = $height;
            $top = $imgDimsMax - $imgWidth;
            $left = $imgDimsMax - $imgWidth;
        }
    } else {
        if( $width > $height ) {
            // set $imgWidth to $imgDimsMax and resize $imgHeight proportionately
            $aspectRatio    = $imgWidth / $width;
            $imgHeight      = floor ( $height * $aspectRatio );
            $top = ( $imgDimsMax - $imgHeight ) / 2;
        } else if( $width < $height ) {
            // set $imgHeight to $imgDimsMax and resize $imgWidth proportionately
            $aspectRatio    = $imgHeight / $height;
            $imgWidth       = floor ( $width * $aspectRatio );
            $left = ( $imgDimsMax - $imgWidth ) / 2;
        }
    }

    return '<img src="' . $image . '" width="' . $imgWidth . '" height="' . $imgHeight . '" style="position:relative;display:inline;top:'.$top.'px;left:'.$left.'px;" />';
}

假设最大图像尺寸为 140 像素。您可以根据自己的意愿更改它。

希望这有帮助。

关于php - 使用 php 直接从 url 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21139492/

相关文章:

php - iPhone - 将图像上传到服务器时如何显示进度条?

matlab - 在 MATLAB 中查找二维脉冲峰值

java - 松香阈值处理(单峰阈值处理)JAVA

php - Laravel:如何使用多个数据透视表关系

php - 时间减去mysql和php

php - 如何使用PHP + mySQL创建一系列DIV?

python - 如何从图像中过滤特定的图像坐标

javascript - 加载js文件在ci bonfire中查看

algorithm - 如何计算该图像中的 Blob 数?

python - 在skimage中裁剪图像?