本博客随机Hatsune Miku图的原理:

  • 采集Danbooru中搜索到的标签为我老婆hatsune miku的图片
  • 将采集到的JSON转为数组
  • 随机取页数及其图片
  • 将图片链接通过GD库输出为jpeg网页图片
  • 链接:miku.hdacgn.com
  • 只生成16:9的壁纸
  • 并且为了美观,防止出现竖屏图片被裁减成一小块,判断原图是否为长大于宽的图片
  • 链接:rand-miku640360.php
  • 为了避免18R内容,搜索时请增加rating:safe

代码:

rand-miku640360.php

<?php
require_once './RandMiku.php';
$RandMiku = new RandMiku();

ini_set('memory_limit', '256M');
A:
$url = 'https://danbooru.donmai.us/posts.json?ms=1&page=' . rand(1, 99) . '&tags=hatsune_miku+rating%3Asafe+&utf8=✓';//✓ = %E2%9C%93
$json = @file_get_contents($url);

$get_array = json_decode($json, true);

$i = 0;
B:
$img_url = $get_array[rand(0, 19)]['file_url'];
if (!is_string($img_url) || $i > 19) goto A;
list($image_o_x, $image_o_y) = $img_url;
if ($image_o_x < $image_o_y){
    $i++;
    goto B;
}

$image = $RandMiku->createImageFromFile($img_url);

$image_x = imagesx($image);
$image_y = imagesy($image);
$new_y = $image_x / 16 * 9;
$image = $RandMiku->cutImage($image, [0, 0], $image_x, $new_y);
$image = $RandMiku->changeImagePixel(640, 360, $image, 640, 360);

header('Content-Type:image/jpeg');
mb_internal_encoding('UTF-8');
imagejpeg($image);
imagedestroy($image);

RandMiku.php

<?php
/**
 * Created by PhpStorm.
 * User: caijianling
 * Date: 2018/9/1
 * Time: 6:00 PM
 */

class RandMiku
{
    /**
     * 设定图片宽高改变图片大小的缩放功能
     * @param int $width 改变宽度为
     * @param int $height 改变高度为
     * @param resource $image 图片资源
     * @param int $high_x 尽可能大的x
     * @param int $high_y 尽可能大的y
     * @return resource 改变后的图像资源
     */
    public function changeImagePixel($width, $height, $image, $high_x = 1080, $high_y = 1829)
    {
        //获取图片宽高
        list($image_width, $image_height) = array(imagesx($image), imagesy($image));
        //创图片背景,大小为要设定的大小
        $image_bg = imagecreatetruecolor($high_x, $high_y);
        //透明色
        $transparent = imagecolorallocatealpha($image_bg, 0, 0, 0, 127);
        //上透明色
        imagefill($image_bg, 0, 0, $transparent);
        //缩放
        imagecopyresampled($image_bg, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
        //输出图像资源
        return $image_bg;
    }

    /**
     * 裁剪图片功能
     * @param resource $image 需要裁剪的图片
     * @param array $new_image_point 裁剪左上角坐标
     * @param int $new_image_w 裁剪后宽度
     * @param int $new_image_h 裁剪后高度
     * @return resource 裁剪后的图像资源
     */
    public function cutImage($image, $new_image_point = array(0, 0), $new_image_w = 0, $new_image_h = 0)
    {
        list($new_image_x, $new_image_y) = $new_image_point;
        //创建背景为裁剪后大小
        $image_bg = imagecreatetruecolor($new_image_w, $new_image_h);
        //透明色
        $transparent = imagecolorallocatealpha($image_bg, 0, 0, 0, 127);
        //填充透明色
        imagefill($image_bg, 0, 0, $transparent);
        //裁剪
        imagecopyresampled($image_bg, $image, 0, 0, $new_image_x, $new_image_y, $new_image_w, $new_image_h, $new_image_w, $new_image_h);
        return $image_bg;
    }

    /**
     * 从文件转成图像资源功能,输出图像资源
     * @param string $image_file
     * @return resource
     */
    public function createImageFromFile($image_file = '')
    {
        list($image_width, $image_height, $image_size) = getimagesize($image_file);
        $image_bg = imagecreatetruecolor($image_width, $image_height);
        $transparent = imagecolorallocatealpha($image_bg, 0, 0, 0, 127);
        imagefill($image_bg, 0, 0, $transparent);
        //图片转成图像资源
        switch ($image_size) {
            case 1:
                $image = imagecreatefromgif($image_file);
                imagecopyresampled($image_bg, $image, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height);
                return $image_bg;
                break;
            case 2:
                $image = imagecreatefromjpeg($image_file);
                imagecopyresampled($image_bg, $image, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height);
                return $image_bg;
                break;
            case 3:
                $image = imagecreatefrompng($image_file);
                imagecopyresampled($image_bg, $image, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height);
                return $image_bg;
                break;
            default:
                // TODO ...
                //如果图片有问题则输出这个图片
                $image_bg_error = imagecreatetruecolor($image_width, $image_height);
                //白色背景
                $white = imagecolorallocate($image_bg_error, 255, 255, 255);
                //填充
                imagefill($image_bg_error, 0, 0, $white);
                //灰色字体
                $gray = imagecolorallocate($image_bg_error, 150, 150, 150);
                imagettftext($image_bg_error, 5, 0, 0, 0, $gray, __DIR__ . '/fonts/Deng.ttf', '错误图片!');
                return $image_bg_error;
        }
    }
}

 

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注

15 + 16 =