Nginx Preliminary-5: Write a C + + extension for PHP with Swig

Source: Internet
Author: User

Performance of PHP-GD

Before writing this chapter, run a PHP script first. As follows:

$SRC = "Test.jpg"; Image optimization used in the picture
$DST = "upload/gray.jpg";//Save result
$totalTime = 0;
$testCount = 32; Can only use such a small value, otherwise run more than 30 seconds
$count = 0;

Attempt to use PHP-GD for
($i = 0; $i < $testCount; $i + +)
{
	$start = Getnanos ();
	$im = Imagecreatefromjpeg ($SRC);
	
	ImageFilter ($im, img_filter_grayscale);	
	
	IMAGEGD ($im, $DST);
	
	Imagedestroy ($im);
	$stop = Getnanos ();

	$time = (max ($start, $stop)-min ($start, $stop))/(1000 * 1000);


	if ($time > 500.0)
	{
		//continue
	}
	else
	{
		$count + +;

		$totalTime + = $time;
	}
}

if ($count = = 0)
	$count = 1;

echo "Perf:". $totalTime/$count. "Ms<br/>";

Do not tube Getnano (), this in the following will give the implementation. The results of this program show that GD needs about half a second for test.jpg such as image processing. This performance can not be said to be very poor, after all, the direct operation of large maps is very rare. But think of "image processing simple optimization" in the results, GD is still very not to force. So, you need to introduce fast processing into PHP, you need to use the swig.


write extensions for PHP to improve performance

There is no doubt that this extension should be named Igame.

Modeled on the functions of GD, a simple definition of Swig interface file, as follows:

%module igame%include <const.i>%include <std_string.i>%{#include "igame.h" #include "image.h" Unsigne
d int Getnanos ();

int Grayscalejpeg (char* src, char* DST);
image* createimagefromjpeg (const std::string& filename);
image* createimagejpeg (unsigned int width, unsigned int height);
int applyimageeffect (image* img);
int imagesaveasjpeg (image* img, const std::string& filename);
void Destroyimage (image* img);
%} extern unsigned int getnanos ();


extern int Grayscalejpeg (char* src, char* DST);
	Class Image {public:image ();
	Image (unsigned int width, unsigned int height);
	Image (const std::string& filename);

Virtual ~image ();
	public:virtual bool Create (unsigned int width, unsigned int height);
	virtual bool Save (const std::string& filename) = 0;
	virtual bool Load (const std::string& filename) = 0;
	virtual bool Close ();
	virtual bool available ();
	virtual unsigned int width ();
	virtual unsigned int height ();
	Virtual unsigned int stride (); Virtual unsigned int bytesperpixel ();
virtual bool Applyeffect (effect* Effect); };
class extern image* createimagefromjpeg (const std::string& filename);
extern image* createimagejpeg (unsigned int width, unsigned int height);
extern int applyimageeffect (image* img);
extern int imagesaveasjpeg (image* img, const std::string& filename); extern void Destroyimage (image* img);

Note that the image class here is exporting a problem and cannot invoke the class's methods. Follow up and deal with it, now it can be used as a container.

The implementation file is as follows:

Igame.h

#ifndef igame_h
#define IGAME_H

#include "image.h"

unsigned int getnanos ();
int Grayscalejpeg (char* src, char* DST);
image* createimagefromjpeg (const std::string& filename);
image* createimagejpeg (unsigned int width, unsigned int height);
int applyimageeffect (image* img);
int imagesaveasjpeg (image* img, const std::string& filename);
void Destroyimage (image* img);
#endif
Igame.cpp

#include "igame.h"
#include "jpeg.h"
#include "grayscale.h"

#include <time.h>


unsigned int Getnanos ()
{
	struct timespec time;

	Clock_gettime (clock_process_cputime_id, &time);

	Return TIME.TV_SEC * 1000 * 1000 + time.tv_nsec;
}


int Grayscalejpeg (char* src, char* dst)
{
	Jpeg myjpg;

	if (Myjpg.load (src))
	{
		grayscale grayscale;

		if (Myjpg.applyeffect (&grayscale))
		{
			if (Myjpg.save (DST)) return
				true;
		}
	}

	return false;
}

image* createimagefromjpeg (const std::string& filename)
{return
	new Jpeg (filename);
}

image* createimagejpeg (unsigned int width, unsigned int height)
{return
	new Jpeg (width, height);
}

int applyimageeffect (image* img)
{
	grayscale grayscale;
	Return Img->applyeffect (&grayscale);
}

int imagesaveasjpeg (image* img, const std::string& filename)
{return
	img->save (filename);
}

void Destroyimage (image* img)
{
	delete img;
}

Modify the php.ini to enable it to use the expansion pack. Then write the same test page, and finally get the result: 136.494ms. is approximately 3 times times more than GD.

Compared to GD's poor performance, this is the intention to develop their own expansion.


A complete Eclipse project can be downloaded from here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.