如何使用 PHP 在下面的代码中设置宽高比

我尝试了 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);

    }

}


ABOUTYOU
浏览 97回答 1
1回答

PIPIONE

尝试这个缩略图函数,它获取源图像资源和缩略图大小,并返回缩略图的新图像资源。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 resourceimagedestroy($source);
打开App,查看更多内容
随时随地看视频慕课网APP