User Tools

Site Tools


crop_resize

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

crop_resize [2014/01/25 11:28] (current)
admin created
Line 1: Line 1:
 +This was adapted from a php script to do the same by Mike Zriel (http://​www.zriel.com/​lamp/​54-crop-and-rescale-images-function-using-gd-library)
  
 +<​code>​
 +bool CompareImages::​cropImageAndRescale (Image image) {
 +    gdImagePtr imageOld, imageNew;
 +    Split splt;
 +    FILE *sourceFd;
 +    vector<​string>​ elements = splt.split(image.sourceImage,'​.'​);​
 +    ​
 +    string ext = elements.back();​
 + if (ext == "​jpg"​ || ext == "​jpeg"​) {
 +            sourceFd = fopen(image.sourceImage.c_str(),"​r"​);​
 + imageOld = gdImageCreateFromJpeg( sourceFd );
 + } else if (ext == "​gif"​) {
 + imageOld = gdImageCreateFromGif( sourceFd );
 + } else if (ext == "​png"​) {
 + imageOld = gdImageCreateFromPng( sourceFd );
 + } else {
 + return false;
 + }
 + int widthOld = gdImageSX(imageOld);​
 + int heightOld = gdImageSY(imageOld);​
 + imageNew = gdImageCreateTrueColor(image.outputWidth,​ image.outputHeight);​
 +        image.print();​
 +        gdImageCopyResized(imageNew,​ imageOld, 0, 0, image.posX, image.posY, image.outputWidth,​ image.outputHeight, ​ image.cropWidth,​ image.cropHeight);​
 +        FILE *out = fopen(image.targetImage.c_str(),​ "​wb"​);​
 +        gdImagePng(imageNew,​ out);
 +        fclose(out);​
 +        fclose(sourceFd);​
 +        gdImageDestroy(imageNew);​
 +        gdImageDestroy(imageOld);​
 +}
 +</​code>​
 +
 +<​code>​
 +#ifndef IMAGE_H
 +#define IMAGE_H
 +#include <​string>​
 +
 +using namespace std;
 +
 +
 +class Image
 +{
 +    public:
 +        string sourceImage;​
 +        string targetImage;​
 +        int cropWidth;
 +        int cropHeight;
 +        int posX;
 +        int posY;
 +        int outputWidth;​
 +        int outputHeight;​
 +        void set( string m_sourceImage,​ string m_targetImage,​ int m_cropWidth,​ int m_cropHeight,​ int m_posX, int m_posY, int m_outputWidth,​ int m_outputHeight) ​
 +        {
 +        sourceImage=m_sourceImage;​
 +        targetImage=m_targetImage;​
 +        cropWidth=m_cropWidth;​
 +        cropHeight=m_cropHeight;​
 +        posX=m_posX;​
 +        posY=m_posY;​
 +        outputWidth=m_outputWidth;​
 +        outputHeight=m_outputHeight;​
 +        }
 +        void print ()
 +        {
 +            cout <<"​cropWidth "<<​ cropWidth<<​endl;​
 +            cout <<"​cropHeight "<<​ cropHeight ​ <<​endl;​
 +            cout <<"​posX "<<​ posX<<​endl;​
 +            cout <<"​posY "<<​ posY<<​endl;​
 +            cout <<"​outputWidth "<<​ outputWidth<<​endl;​
 +            cout <<"​outputHeight "<<​ outputHeight<<​endl;​
 +        }
 +};
 +#endif
 +</​code>​
 +
 +Examples ​
 +
 +<​code>​
 +    Image im;
 +//void set( string m_sourceImage,​ string m_targetImage,​ int m_cropWidth,​ int m_cropHeight,​ int m_posX, int m_posY, int m_outputWidth,​ int m_outputHeight) ​
 +    im.set("/​run/​shm/​goal.jpg","/​home/​pi/​smgoal.jpg",​450,​130,​0,​70,​450,​333);​
 +    compareImages.cropImageAndRescale(im);​
 +    im.set("/​run/​shm/​goal.jpg","/​home/​pi/​xsmgoal.jpg",​450,​330,​0,​0,​225,​166);​
 +    compareImages.cropImageAndRescale(im);​
 +</​code>​
crop_resize.txt ยท Last modified: 2014/01/25 11:28 by admin