我不确定这是否可以完成,所以在这里发布。我有一个图像存储在网络服务器中。例如 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/