Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将图像大小调整到特定高度_Php - Fatal编程技术网

Php 将图像大小调整到特定高度

Php 将图像大小调整到特定高度,php,Php,我目前已经实现了一段代码,允许我调整图像的大小,以允许在我正在创建的图库中显示缩略图,尽管进展不太顺利 我正在使用一些预先编写的代码来调整图像的大小,尽管我正在努力使图像的最小高度为195px,宽度为195px,如果可能的话,保持比例不变 还有什么方法可以优化图像,从而提高页面加载时间 这是我目前的代码,任何帮助都将不胜感激,谢谢 function imageResize() { $filename = $row['main']; if (exif_imagetype($filename) =

我目前已经实现了一段代码,允许我调整图像的大小,以允许在我正在创建的图库中显示缩略图,尽管进展不太顺利

我正在使用一些预先编写的代码来调整图像的大小,尽管我正在努力使图像的最小高度为195px,宽度为195px,如果可能的话,保持比例不变

还有什么方法可以优化图像,从而提高页面加载时间

这是我目前的代码,任何帮助都将不胜感激,谢谢

function imageResize() {
$filename = $row['main'];

if (exif_imagetype($filename) == IMAGETYPE_GIF) {
    $create = imagecreatefromgif;
}

if (exif_imagetype($filename) == IMAGETYPE_JPEG) {
    $create = imagecreatefromjpeg;
}

if (exif_imagetype($filename) == IMAGETYPE_PNG) {
    $create = imagecreatefrompng;
}

$width = 600; //These values have been tinkered with from their original values
$height = 500;

header('Content-Type: image/jpeg');

list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($height_orig < 300) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

$image_p = imagecreatetruecolor($width, $height);
$image = $create($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, null, 100);
}

找出原始图像中哪个较小,高度还是宽度。如果此值小于195像素的最小值,请将其设置为该最小值,并根据图像的原始纵横比缩放其他值:

$minSize = 195;
$aspectRatio = $width_orig / $height_orig;

if ($width_orig < $height_orig) {
    if ($width_orig < $minSize) {
        $width = $minSize;
        $height = $width / $aspectRatio;
    }
} else {
    if ($height_orig < $minSize) {
        $height = $minSize;
        $width = $height * $aspectRatio;
    }
}

那么,问题是什么呢?还有$create$filename是打字错误吗?它应该是create$filename我的图像不会以最小195px的速度出现,而且$create是一个变量,因此去掉$create可能会导致它崩溃:-可能不是最合适的名称变量。