ImageMagick++でサムネールに変換
C++はなんども挫折してるのだが、サムネール処理がいる、というのでMagick++を使ってみた。
#include <Magick++.h> #include <iostream> class ImageFactory { public: ImageFactory(int width, int height); ~ImageFactory(); void thumbnail(const char *source_image, const char *thumb_image); private: Magick::Image image; Magick::Geometry size; int _width; int _height; float _ratio; };
#include "ImageFactory.h" using namespace std; using namespace Magick; ImageFactory::ImageFactory(int width, int height) { _width = width; _height = height; _ratio = (float) width / (float) height; }; ImageFactory::~ImageFactory() { }; void ImageFactory::thumbnail(const char *source_image, const char *thumb_image) { float ratio; try { // Read a file into image object image.read(source_image); size = image.size(); if (size.height() != 0) { ratio = (float) size.width() / (float) size.height(); } // Crop the image to specified size (width, height, xOffset, yOffset) if (ratio > _ratio) { // 横長: image.zoom(Geometry((int) (_height * ratio), _height, 0, 0)); image.crop(Geometry(_width, _height, (int) ((image.size().width() - _width) / 2), 0)); } else { // 縦長: image.zoom(Geometry(_width, (int) (_width / ratio), 0, 0)); image.crop(Geometry(_width, _height, 0, (image.size().height() - _height) / 2)); } image.magick("png"); // Write the image to a file image.write(thumb_image); } catch (Exception &error_) { cout << "Caught exception: " << error_.what() << endl; } };