Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

How to create thumbnail file with CutyCapt and php

Published on| February 15th, 2011 | 8 Comments.
Description:

I have used software(CutyCapt) in previous post( "How to execute Qt's application ( CutyCapt ) on Windows."), and I will use same software in this post too.
So I will create thumbnail images of some websites by software(CutyCapt) and php programing.

CutyCapt might help you to create hard copy image of some websites, with very easy your operations.(See previous post ).
I will zoom or cut some images that created by CutyCapt in this post, so by PHP programing.

Sample source code I used:


1st step, Let's try using CutyCapt.

You will download CutyCapt from the following URL.
Download Locations: http://Cutycapt.sourceforge.net/

Let's try use CutyCapt by command line, like the following command.

> CutyCapt --url=http://www.yahoo.co.jp --out=yahoo.jpg

If you can find "yahoo.jpg" image file in current directry, it's OK.
* You should set 'PATH' of "CutyCapt".

You will be able to set "PATH" of "CutyCapt" on Windows(DOS) by a following command.
> set PATH=%PATH%;C:\CutyCapt
> echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\CutyCapt
C:\CutyCapt : Derectory of CutyCapt exec file.

You will be able to set "PATH" of "CutyCapt" on Linux by a following command.
> set PATH=$PATH:$HOME/bin
> echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/hoge/bin
/home/hoge/bin : Derectory of CutyCapt exec file.

2nd step, Check on the packages needed by PHP for image file.

PHP will require GD package for handling the image file.
You will be able to check if it had installed in your system by the following operation.

  1. Upload the file that include the following PHP code in directory of website.
    <?php
    echo phpinho();
    ?>
  2. Access to URL that uploaded PHP file.

    You will see a similar image of above. It means that your system was installed GD package.
If you have not environment for web server, you will be able to also check by following command.
> php -r "echo phpinfo();"
 
      :
      :
      :
 
gd
 
GD Support => enabled
GD Version => bundled (2.0.34 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.1.9
T1Lib Support => enabled
GIF Read Support => enabled
GIF Create Support => enabled
JPG Support => enabled
PNG Support => enabled
WBMP Support => enabled
XBM Support => enabled
* Above information is too many informations. You should print out to a text file, and you will be able to check "GD".
If your system was not installed "GD", you should install by following steps.

If you use Windows OS,
you should re-write 'php.ini'. Just only you will erase comment out code ";" like the following code.
extension=php_gd2.dll
If you use Linux(Red Hat) OS,
you should use 'yum' like following command.
> yum install php-gd
3rd step,Create thumbnail images by PHP.

You will edit programing by PHP for thumbnail images. Previous step, you should think following function by PHP.

  1. PHP's function will accept the following two parameters.
    • URL address for website page thumbnail
    • Output Image file name (the extension of gif, jpeg, png supported )
  2. CutyCapt will create hard copy image of a website page from the inputted URL,
    * This original hard copy image file name will be 2nd parameter's "file name" + .org.png .
    This file is always PNG format.
  3. Next, PHP function will create two thumbnail images from hard copy file that CutyCapt has created.
    • Larger image (400 x 400)
    • Small image (140 x 100)
    The original hard copy image will be usually too big. So, "Larger image" will be used for expansion of small image thumbnail.
    And 2nd parameter's "file name" of PHP function will be used as file name of "Larger image".
    The file name of "Small image" will use 2nd parameter's "file name" + '.s' + expansion.

The following code is sample PHP code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
//	global parameters
$thumb_small_width  = 140;	//	small image width
$thumb_small_height = 100;	//	small image height
 
$thumb_big_width  = 400;	//	large image width
$thumb_big_height = 400;	//	large image height
 
$jpeg_quality = 100;	//	image quality for jpeg
 
//	main routine
if (   ( $argc >= 2 && in_array($argv[1], array('--help', '-help', '-h', '-?'))) 
    || ( $argc < 3 )  ) {
    //	show help
	echo $argv[0];
	echo 
' --help
usage
  php autothumb.php URL OUTPUT-FileName
 
parameters
 URL : URL Name ex) http://www.yahoo.co.jp
 OUTPUT-FileName : OUTPUT Image FileName ex) C:\temp\yahoo.jpg
';
 
} else {
    //	check parameters
	$urlname  = $argv[1];	//	URL Name
	$filename = $argv[2];	//	Output Image File Name
 
	$error_f = 0;
	$prm_error = '';
	if(!empty($urlname)){
		if(stripos($urlname,'http')==0){
			//	ok
		} else {
			$prm_error = "You should set protocol-name('ex)http://') before URL address.\n";
			$error_f++;
		}
	} else {
		$prm_error = "You should set URL Name.\n";
		$error_f++;
	}
	//	check extension type
	$extension_type = -1;
	$extension_char = '';
	if(strrpos($filename,'.')!==false){
		$extfile = strtolower(substr($filename,strrpos($filename,'.')));
		if( $extfile == ".jpeg" || $extfile == ".jpg" ){
			$extension_type = 1;
			$extension_char = '.jpg';
		} else if( $extfile==".png" ){
			$extension_type = 2;
			$extension_char = '.png';
		} else if( $extfile==".gif" ){
			$extension_type = 3;
			$extension_char = '.gif';
		}
	}
 
	switch($extension_type){
	case 0:
		$prm_error  = "*** Warning ***\n";
		$prm_error  = "You should set extension-name('ex) .jpg') after OUTPUT-FileName.\n";
		$prm_error .= "But this program automated addition function will set extension-name (.jpg).\n";
		$filename .= '.jpg';
		$extension_type = 1;
		$extension_char = '.jpg';
		break;
	case -1:
		$prm_error = "You should set OUTPUT-FileName(Image File Name).\n";
		$error_f++;
		break;
	default:
		//	none
		break;
	}
	echo $prm_error;
	if($error_f==0){
		echo "[start]URL=$urlname  FILE=$filename\n";
 
		//	set image file names.
		$cached_filename    = $filename . '.org.png';
		$cached_filename_S  = $filename . '.s' . $extension_char;
		$cached_filename_L  = $filename;
 
		//	remove image files
		if(file_exists($cached_filename)){
			unlink ($cached_filename);
		}
		if(file_exists($cached_filename_S)){
			unlink ($cached_filename_S);
		}
		if(file_exists($cached_filename_L)){
			unlink ($cached_filename_L);
		}
 
		// Get website image and save it on the server.
		@exec('CutyCapt.exe ' . escapeshellarg('--url='.$urlname) . ' ' . escapeshellarg('--out='.$cached_filename));
 
		if (!file_exists($cached_filename)) {
			echo $filename." Web Page Image Hard Copy NG. The Image File was not created.\n";
		} else {
			if(!file_exists($cached_filename_S)){
				//	get original image file attribute
				list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($cached_filename);
 
				// Create a new image from file or URL
				$org_image   = ImageCreateFromPng($cached_filename);
 
				//	org file height
				$orig_y_S = ($orig_x/$thumb_small_width) * $thumb_small_height;
				if($orig_y_S>$orig_y){
					$thumb_small_height = $thumb_small_height * ($orig_y/$orig_y_S);
					$orig_y_S = $orig_y;
				}
				$orig_y_L = ($orig_x/$thumb_big_width) * $thumb_big_height;
				if($orig_y_S>$orig_y){
					$thumb_big_height = $thumb_big_height * ($orig_y/$orig_y_L);
					$orig_y_L = $orig_y;
				}
 
				// Create a new true color image
				$new_image   = ImageCreateTrueColor($thumb_small_width,$thumb_small_height);
				$new_image_L = ImageCreateTrueColor($thumb_big_width,  $thumb_big_height  );
 
				// Copy and resize part of an image with resampling
				imagecopyresampled(
					$new_image, // Destination image link resource. 
					$org_image, // Source image link resource.
					0, 0,       // destination point.(x,y) 
					0, 0,       // source point.(x,y) 
					$thumb_small_width, $thumb_small_height,   // Destination (width,height). 
					$orig_x, $orig_y_S);  // Source (width,height)
 
				imagecopyresampled(
					$new_image_L, // Destination image link resource. 
					$org_image,   // Source image link resource.
					0, 0,         // destination point.(x,y) 
					0, 0,         // source point.(x,y) 
					$thumb_big_width, $thumb_big_height,       // Destination (width,height). 
					$orig_x, $orig_y_L);  // Source (width,height)
 
				//	image file save
				switch($extension_type){
				case 2:	//	png
					$res = ImagePNG($new_image,  $cached_filename_S);
					$res = ImagePNG($new_image_L,$cached_filename_L);
					break;
				case 3:	//	gif
					$res = ImageGIF($new_image,  $cached_filename_S);
					$res = ImageGIF($new_image_L,$cached_filename_L);
					break;
				default:	//	1 or else jpeg
					$res = ImageJPEG($new_image,  $cached_filename_S,$jpeg_quality);
					$res = ImageJPEG($new_image_L,$cached_filename_L,$jpeg_quality);
					break;
				}
			}
		}
		echo "[end]URL=$urlname  FILE=$filename\n";
	}
}
?>

I will give you the following simple description:

Line 3 - 4 Set width and height of Thumbnail image.
Line 6 - 7 Set width and height of Large Thumbnail image.
Line 9 Set quality of JPEG.
Line 45 - 59 Check the image format by the file name extension, and set it.
Line 83 - 85 Set image file names.
Line 88 - 96 If same image file name exist, it will delete it.
Line 99 CutyCapt will execute, and will create image file of the website page.
Line 112 - 121 Adjust height of the image file.
Line 128 - 142 Create a thumbnail image.
Line 145 - 158 Save thumbnail image to files.

You will use like the following.
You should replace URL and the output image file name on the following command.

> php autothumb2.php http://www.yahoo.co.jp yahoo.jpg
[start]URL=http://www.yahoo.co.jp  FILE=yahoo.jpg
[end]URL=http://www.yahoo.co.jp  FILE=yahoo.jpg
> 

By this command result, you will get three image files.

  • yahoo.jpg.org.png -- This original image file is that created by CutyCapt.
  • yahoo.jpg.s.jpg -- This image file is "Small Image" thumnail file.
  • yahoo.jpg -- This image file is "Large Image" thumnail file.
You will get easy way for creating thumbnail website page.

So, this thumbnail images might use like sample page view of Google search result pages. ( But you might not like its function of Google... , because the function given too much stress to web server. )



Comments

8 Comments. “How to create thumbnail file with CutyCapt and php”


  1. Niladri
    June 11th, 2011 @ 20:25:05

    I want to know that is CutyCapt capable create thumbnails in Linux based server or not?

  2. master
    June 12th, 2011 @ 04:08:37

    Yes , you can do it if you have environment of Desktop and Qt development on Linux.
    This software might be used on Linux rather than Windows.

  3. James
    June 20th, 2011 @ 10:35:00

    The English on this page is very hard to understand.

  4. master
    June 23rd, 2011 @ 16:54:45

    I edited this English page.
    still hard ?

  5. mmc
    August 12th, 2011 @ 04:59:48

    the english is fine! thanks for the effort and details!

  6. master
    August 12th, 2011 @ 09:47:47

    This is Admin.
    Thanks.

  7. Manoj
    April 9th, 2012 @ 19:12:44

    I have configured CutyCapt.exe on my server and this is working for all http:// request url;

    But when i try to capture any https://url to capture image this gives me a blank image after long time processing.

    Please can you help me.

    Thanks!!

  8. Trent
    August 16th, 2012 @ 03:05:53

    I realize this post is over a year old, but I wanted to leave a note for the creator saying ‘THANK YOU’! This has helped me tremendously!!!!

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ