LoginSignup
2
1

More than 5 years have passed since last update.

Imagick twitterアイコン用gif加工

Last updated at Posted at 2013-03-14

とりあえず初投稿。
http://hyahha.orz.hm でデモできます。

ImageMagickのインストール方法は省略

ImageMagickのphpモジュールのインストール
 pecl install Imagick
 php -r "phpinfo();" | grep "php.ini"
 echo "extension=imagick.so" >> "php.iniのパス"
twitgif.php
<?php
/**
 * @file
 * GIFアニメーションをTwitter用にリサイズ、圧縮する。
 */

set_time_limit(60);
session_start();
require_once("twitteroauth/twitteroauth.php");
require_once("config.php");
require_once("twitter_user.php");

if ($_SERVER["REQUEST_METHOD"] != "POST" || !isset($_FILES["gif_data"])) {
  header("Status: 400 Bad Request");
  die("400 Bad Request");
}

if (isset($_POST["crop_size"]) && $_POST["crop_size"] == 48) {
  define("GIF_CROP_SIZE", 48);
} else {
  define("GIF_CROP_SIZE", 73);
}

$image = $_FILES["gif_data"];
$screen_name = $user->screen_name;

if ($image["error"] != UPLOAD_ERR_OK) {
  header("Status: 500 Internal Server Error");
  die("upload error");
}
if ($image["type"] != "image/gif") {
  header("Status: 400 Bad Request");
  die("this uploaded file type is not gif");
}

$uniqid = uniqid();

if (strlen($screen_name) < 3) {
  $sub_dir = DOCUMENT_ROOT . "/uploads/other";
} else {
  $sub_dir = DOCUMENT_ROOT . "/uplaods/" . substr($screen_name, 0, 2);
}

if (!is_dir($sub_dir)) {
  mkdir($sub_dir);
}

$user_dir = $sub_dir . "/" . $screen_name;

$file_path = $user_dir . "/" . $uniqid . ".org.gif";
move_uploaded_file($image["tmp_name"], $file_path);

$imagick = new Imagick($file_path);

foreach ($imagick as $im) {
  $im->cropThumbnailImage(GIF_CROP_SIZE, GIF_CROP_SIZE);
}

$gif_path = $user_dir . "/" . $uniqid . ".gif";

$imagick->setCompression(Imagick::COMPRESSION_LZW);
$imagick->quantizeImages(32, Imagick::COLORSPACE_RGB, 0, false, false);
$imagick->writeImages($gif_path, true);
$imagick->destroy();

unlink($file_path);

$gif_url = str_replace(DOCUMENT_ROOT, $host_name, $gif_path);

include("twitgif.inc");
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1