【问题标题】:How to set aspect ratio in below code using PHP如何使用 PHP 在下面的代码中设置纵横比
【发布时间】:2020-10-18 15:58:50
【问题描述】:

我尝试了 2 个实现,如下所示。两者都运行良好,但是当我尝试将它们混合时,它不再起作用了。

$output['status']=FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif",   "image/jpeg",   "image/pjpeg",   "image/png",   "image/x-png"  );

if ($_FILES['image_file_input']["error"] > 0) {
    $output['error']= "File Error";
}
elseif (!in_array($_FILES['image_file_input']["type"], $allowedImageType)) {
    $output['error']= "Invalid image format";
}
elseif (round($_FILES['image_file_input']["size"] / 1024) > 4096) {
    $output['error']= "Maximum file upload size is exceeded";
} else {
    $temp_path = $_FILES['image_file_input']['tmp_name'];
    $file = pathinfo($_FILES['image_file_input']['name']);
    $fileType = $file["extension"];
    $photo_name = $productname.'-'.$member_id."_".time();
    $fileName1 = $photo_name . '-125x125' . ".jpg";
    $fileName2 = $photo_name . '-250x250' . ".jpg";
    $fileName3 = $photo_name . '-500x500' . ".jpg";
    
    $small_thumbnail_path = "uploads/large/";
    createFolder($small_thumbnail_path);
    $small_thumbnail = $small_thumbnail_path . $fileName1;
    
    $medium_thumbnail_path = "uploads/large/";
    createFolder($medium_thumbnail_path);
    $medium_thumbnail = $medium_thumbnail_path . $fileName2;
    
    $large_thumbnail_path = "uploads/large/";
    createFolder($large_thumbnail_path);
    $large_thumbnail = $large_thumbnail_path . $fileName3;
    
    $thumb1 = createThumbnail($temp_path, $small_thumbnail,$fileType, 125, 125  );
    $thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 250, 250);
    $thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 500, 500);
            
    if($thumb1 && $thumb2 && $thumb3) {
        $output['status']=TRUE;
        $output['small']= $small_thumbnail;
        $output['medium']= $medium_thumbnail;
            $output['large']= $large_thumbnail;
    }
}
echo json_encode($output);

函数文件

function createFolder($path)
{
    if (!file_exists($path)) {
        mkdir($path, 0755, TRUE);
    }
}

function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
    
    $source = imagecreatefromjpeg($sourcePath);
    
    $width = imagesx($source);
    $height = imagesy($source);
    
    $tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
    
    imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
    
    if (imagejpeg($tnumbImage, $targetPath, 90)) {
        imagedestroy($tnumbImage);
        imagedestroy($source);
        return TRUE;
    } else {
        return FALSE;
    }
}

纵横比代码,这是我尝试混合此代码的另一个。但我没有成功

$fn = $_FILES['image']['tmp_name'];
$size = getimagesize($fn);
$ratio = $size[0]/$size[1]; // width/height
$photo_name = $productname.'-'.$member_id."_".time();
{
    if( $ratio > 1) {
    $width1 = 500;
    $height1 = 500/$ratio;
}
else {
    $width1 = 500*$ratio;
    $height1 = 500;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width1,$height1);
$fileName3 = $photo_name . '-500x500' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width1,$height1,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName3); // adjust format as needed
imagedestroy($dst);
    if( $ratio > 1) {
    $width2 = 250;
    $height2 = 250/$ratio;
}
else {
    $width2 = 250*$ratio;
    $height2 = 250;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width2,$height2);
$fileName2 = $photo_name . '-250x250' . ".jpg";
imagecopyresampled($dst,$src,0,0,0,0,$width2,$height2,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst,$fileName2); // adjust format as needed
imagedestroy($dst);
}

我需要的是在调整大小后保存我的图像,但在第二个代码中没有条件检查,我无法获取图像上传文件夹路径。这就是为什么我需要合并这两个代码。

基本上我需要以 3 种尺寸格式保存图像:500x500、250x250 和 125x125。宽度固定,但高度根据纵横比设置,并在第二个代码块中设置上传文件夹和条件。

【问题讨论】:

  • 但是当我尝试混合两种代码时它不起作用混合不起作用是什么意思?
  • 也没有关注你@Dhruv。我了解您正在尝试合并 2 个不同的代码块,因为您想要两个块中的逻辑,但实际上并不清楚您要实现什么以及什么不起作用?
  • @Prof83 我想在调整大小后保存我的图像,在第二个代码中没有条件检查,我无法获取图像上传文件夹路径,这就是我需要合并 2 个代码的原因
  • @Prof83 基本上我需要将我的图像保存为 3 种尺寸 500x500,250x250 和 125x125 ,宽度是固定的,但高度是根据纵横比设置的,并在第二个代码块中设置上传文件夹和条件跨度>
  • 我修复了一些格式错误,尝试修复描述并将 OP 的 cmets 合并到问题中。

标签: php image-processing


【解决方案1】:

试试这个缩略图函数,它获取你的源图像资源和缩略图大小,并为缩略图返回一个新的图像资源。

function createThumbnail($src, int $width = 100, int $height = null) {

    // Ensure that src is a valid gd resource
    if (!(is_resource($src) && 'gd' === get_resource_type($src))) {
        throw new InvalidArgumentException(
            sprintf("Argument 1 of %s expected type resource(gd), %s supplied", __FUNCTION__, gettype($src))
        );
    }

    // Extract source image width, height and aspect ratio
    $source_width = imagesx($src);
    $source_height = imagesy($src);
    $ratio = $source_width / $source_height;

    // We know that width is always supplied, and that height can be null
    // We must solve height according to aspect ratio if null is supplied
    if (null === $height) {
        $height = round($width / $ratio);
    }

    // Create the thumbnail resampled resource
    $thumb = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
    return $thumb;

}

鉴于您上面的代码,您现在可以像这样使用该函数


// Get uploaded file information as you have

// Create your source resource as you have
$source = imagecreatefromstring(file_get_contents($fn));

// Create thumbnails and save + destroy

$thumb = createThumbnail($source, 500);
imagejpeg($thumb, $thumb500TargetPath, 90);
imagedestroy($thumb);

$thumb = createThumbnail($source, 250);
imagejpeg($thumb, $thumb250TargetPath, 90);
imagedestroy($thumb);

$thumb = createThumbnail($source, 125);
imagejpeg($thumb, $thumb125TargetPath, 90);
imagedestroy($thumb);

// Don't forget to destroy the source resource
imagedestroy($source);

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2015-05-02
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多