在php中使用最大高度和宽度调整图像大小
好的,我正在尝试使用php调整图像的大小。我无法在目前使用的服务器上安装插件在php中使用最大高度和宽度调整图像大小,php,image,Php,Image,好的,我正在尝试使用php调整图像的大小。我无法在目前使用的服务器上安装插件 $originalimage是我要调整大小的实际原始图像。 $width是定义$original\u width的另一个参数。这同样适用于$height和$original\u height $original_width = imagesx($originalimage); $original_height = imagesy($originalimage); $max_width = $thumbwidth; $
$originalimage
是我要调整大小的实际原始图像。
$width
是定义$original\u width
的另一个参数。这同样适用于$height
和$original\u height
$original_width = imagesx($originalimage);
$original_height = imagesy($originalimage);
$max_width = $thumbwidth;
$max_height = $thumbheight;
$width = $original_width;
$height = $original_height;
使用上面的预设置,我开始在这里工作。这可以工作,但无法设置最大高度。例如,我将最大宽度传递为$thumbwidth
,最大高度传递为$thumbheight
,这将根据需要工作,直到我尝试继续并使用高于宽度的图像。(纵向图像。)它并没有完全失败,但它确实无法强制执行最大高度,从而渲染可能非常高的图像
if ($width > $height) {
$newwidth = $thumbwidth;
$divisor = $width / $thumbwidth;
$newheight = floor( $height / $divisor);
} else {
$newheight = $thumbheight;
$divisor = $height / $thumbheight;
$newwidth = floor( $width / $divisor );
}
$image = imagecreatetruecolor( $newwidth, $newheight );
imagecopyresampled( $image, $originalimage, 0, 0, 0, 0, $newwidth, $newheight, $original_width, $original_height );
在尝试理解这一点并在几个小时后失败后,我想出了下面的代码,这是我从php.net获得的。正如您所见,我在代码的预设置部分设置了两组彼此相等的变量是有原因的。主要是因为我无法理解在第二个代码段中将$max_width
调用为$thumbwidth
。
在您传入一个大于$originalimage
的宽度或高度的参数之前,下面的操作同样有效
# taller
if ($height > $max_height) {
$width = ($max_height / $height) * $width;
$height = $max_height;
}
# wider
if ($width > $max_width) {
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image = imagecreatetruecolor( $width, $height );
imagecopyresampled( $image, $originalimage, 0, 0, 0, 0, $width, $height, $original_width, $original_height );
很抱歉,我自己找不到一个方法来做这件事。至于这是一个重复的问题,大多数类似的问题都会以“[Insert plugin name here]更好地使用它”结尾。我特别要求的是一个不使用其他插件或javascript的答案。(除了预装在我的服务器上的GD之外。)
在我结束这个问题之前,我会说我正在使用
imagejpeg($image)因此,HTML或CSS的使用是完全禁止的。这是我为一个程序设计的一个类。也许对你有帮助
<?php
class ImageHelper
{
/**
* @static
* @param $source string Path for source image
* @param $destination string Path for destination image to be placed
* @param $targetWidth int Width of the new image (in pixels)
* @param $targetHeight int Height of the new image (in pixels)
* @param $strict bool
*/
public static function createImage($source, $destination, $targetWidth, $targetHeight, $strict = false){
$dir = dirname($destination);
if(!is_dir($dir)){
mkdir($dir, 0770, true);
}
$fileContents = file_get_contents($source);
$image = imagecreatefromstring($fileContents);
$thumbnail = ImageHelper::resizeImage($image, $targetWidth, $targetHeight, $strict);
imagejpeg($thumbnail, $destination, 100);
imagedestroy($thumbnail);
imagedestroy($image);
}
/**
* Resize an image to the specified dimensions
* @param string $original Path to the original image
* @param int $targetWidth Width of the new image (in pixels)
* @param int $targetHeight Height of the new image (in pixels)
* @param bool $strict True to crop the picture to the specified dimensions, false for best fit
* @return bool|resource Returns the new image resource or false if the image was not resized.
*/
public static function resizeImage($original, $targetWidth, $targetHeight, $strict = false)
{
$originalWidth = imagesx($original);
$originalHeight = imagesy($original);
$widthRatio = $targetWidth / $originalWidth;
$heightRatio = $targetHeight / $originalHeight;
if(($widthRatio > 1 || $heightRatio > 1) && !$strict){
// don't scale up an image if either targets are greater than the original sizes and we aren't using a strict parameter
$dstHeight = $originalHeight;
$dstWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcWidth = $originalWidth;
$srcX = 0;
$srcY = 0;
}elseif ($widthRatio > $heightRatio) {
// width is the constraining factor
if ($strict) {
$dstHeight = $targetHeight;
$dstWidth = $targetWidth;
$srcHeight = $originalHeight;
$srcWidth = $heightRatio * $targetWidth;
$srcX = floor(($originalWidth - $srcWidth) / 2);
$srcY = 0;
} else {
$dstHeight = ($originalHeight * $targetWidth) / $originalWidth;
$dstWidth = $targetWidth;
$srcHeight = $originalHeight;
$srcWidth = $originalWidth;
$srcX = 0;
$srcY = 0;
}
} else {
// height is the constraining factor
if ($strict) {
$dstHeight = $targetHeight;
$dstWidth = $targetWidth;
$srcHeight = $widthRatio * $targetHeight;
$srcWidth = $originalWidth;
$srcY = floor(($originalHeight - $srcHeight) / 2);
$srcX = 0;
} else {
$dstHeight = $targetHeight;
$dstWidth = ($originalWidth * $targetHeight) / $originalHeight;
$srcHeight = $originalHeight;
$srcWidth = $originalWidth;
$srcX = 0;
$srcY = 0;
}
}
$new = imagecreatetruecolor($dstWidth, $dstHeight);
if ($new === false) {
return false;
}
imagecopyresampled($new, $original, 0, 0, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
return $new;
}
}
您可以按如下所示尝试此功能
<?php
function makeThumbnail($sourcefile,$max_width, $max_height, $endfile, $type){
// Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it
// and places it at endfile (path/to/thumb.jpg).
// Load image and get image size.
//
switch($type){
case'image/png':
$img = imagecreatefrompng($sourcefile);
break;
case'image/jpeg':
$img = imagecreatefromjpeg($sourcefile);
break;
case'image/gif':
$img = imagecreatefromgif($sourcefile);
break;
default :
return 'Un supported format';
}
$width = imagesx( $img );
$height = imagesy( $img );
if ($width > $height) {
if($width < $max_width)
$newwidth = $width;
else
$newwidth = $max_width;
$divisor = $width / $newwidth;
$newheight = floor( $height / $divisor);
}
else {
if($height < $max_height)
$newheight = $height;
else
$newheight = $max_height;
$divisor = $height / $newheight;
$newwidth = floor( $width / $divisor );
}
// Create a new temporary image.
$tmpimg = imagecreatetruecolor( $newwidth, $newheight );
imagealphablending($tmpimg, false);
imagesavealpha($tmpimg, true);
// Copy and resize old image into new image.
imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Save thumbnail into a file.
//compressing the file
switch($type){
case'image/png':
imagepng($tmpimg, $endfile, 0);
break;
case'image/jpeg':
imagejpeg($tmpimg, $endfile, 100);
break;
case'image/gif':
imagegif($tmpimg, $endfile, 0);
break;
}
// release the memory
imagedestroy($tmpimg);
imagedestroy($img);
}
?>
下载演示代码我在构建图库页面的拇指时遇到了类似的问题。我尝试了上面的Daniel Nyamasyo解决方案,但就是无法通过“$img=imagecreatefromjpeg($sourcefile);”行而不获得“警告:imagecreatefromjpeg无法打开流:HTTP请求失败!HTTP/1.1 400错误请求”,无论我输入了什么$sourcefile。
我想出了这个肮脏的方法
<?php
// Some variables
$thumb_width = 162; // The maximum values you want
$thumb_height = 122;
$thumb_pointer = 'thumbs'; // Put your output folder here which must exist
$img = imagecreatefromjpeg( $image_name);
$width = imagesx( $img );
$height = imagesy( $img );
if ($width>=$height)
{
// Now calculate thumbnail size
$new_width = $thumb_width;
$new_height = floor( $height * ($thumb_width / $width));
// The 'dirty' bit I found was needed for square or near square images
while ($new_height > $thumb_height)
{
$new_width = floor($new_width * 0.99);
$new_height = floor($new_height * 0.99);
}
}
else
{
$new_height = $thumb_height;
$new_width = floor($width * ($thumb_height / $height));
}
// Create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// Copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// Save the thumbnail into a file
imagejpeg( $tmp_img, $thumb_pointer.'/'.$image_name);
?>
使用white hat的php大小调整脚本它工作起来就像一个梦我刚刚看过它,可能很好,但我需要它。。。就像上面的例子一样。我还不打算使用任何文件包含或函数。我试图让我的脚本线性运行,而不必依赖其他文件。我在white hat的php大小调整脚本中看到了这些函数,但我似乎无法将它们从脚本中拉出来并单独使用它们。尽管这不是我的总体选择,但了解这些对我理解图像大小调整有很大帮助。足够了,所以我编写了自己独特的算法来确定应该重新调整的大小。(基于高度或基于宽度?问题)
<?php
// Some variables
$thumb_width = 162; // The maximum values you want
$thumb_height = 122;
$thumb_pointer = 'thumbs'; // Put your output folder here which must exist
$img = imagecreatefromjpeg( $image_name);
$width = imagesx( $img );
$height = imagesy( $img );
if ($width>=$height)
{
// Now calculate thumbnail size
$new_width = $thumb_width;
$new_height = floor( $height * ($thumb_width / $width));
// The 'dirty' bit I found was needed for square or near square images
while ($new_height > $thumb_height)
{
$new_width = floor($new_width * 0.99);
$new_height = floor($new_height * 0.99);
}
}
else
{
$new_height = $thumb_height;
$new_width = floor($width * ($thumb_height / $height));
}
// Create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// Copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// Save the thumbnail into a file
imagejpeg( $tmp_img, $thumb_pointer.'/'.$image_name);
?>