DCL 3.7.4
Loading...
Searching...
No Matches
ThumbNail.cpp
Go to the documentation of this file.
1#include <stdio.h>
2#include <gd.h>
3#include <dcl/core/String.h>
4
5#if __DCL_HAVE_THIS_FILE__
6#undef __THIS_FILE__
7static const char_t __THIS_FILE__[] = L"fastpage/ThumbNail.cpp";
8#endif
9
10__DCL_BEGIN_NAMESPACE
11
12// result ContentType
13// image/jpeg
14
16 const char* pszSrcFile,
17 const char* pszContentType,
18 const char* pszThumbnailFile,
19 int nSize // = 110
20)
21{
22 __DCL_ASSERT(pszSrcFile != NULL);
23 __DCL_ASSERT(pszContentType != NULL);
24 __DCL_ASSERT(pszThumbnailFile != NULL);
25
26 for( ; *pszContentType; pszContentType++) {
27 if (*pszContentType == '/')
28 {
29 pszContentType++;
30 break;
31 }
32 }
33
34#define IMAGE_UNKNOWN 0
35#define IMAGE_JPEG 1
36#define IMAGE_GIF 2
37#define IMAGE_PNG 3
38
39 int nSrcType = IMAGE_UNKNOWN;
40 if (String::compareNoCase(pszContentType, L"jpeg") == 0
41 || String::compareNoCase(pszContentType, L"pjpeg") == 0)
42 nSrcType = IMAGE_JPEG;
43 else if (String::compareNoCase(pszContentType, L"gif") == 0)
44 nSrcType = IMAGE_GIF;
45 else if (String::compareNoCase(pszContentType, L"png") == 0)
46 nSrcType = IMAGE_PNG;
47
48 if (nSrcType == IMAGE_UNKNOWN)
49 return false;
50
51 gdImagePtr inImage = NULL;
52 FILE* inFile = fopen(pszSrcFile, L"rb");
53 if (!inFile)
54 return false;
55
56 switch(nSrcType) {
57 case IMAGE_JPEG :
58 inImage = gdImageCreateFromJpeg(inFile);
59 break;
60 case IMAGE_GIF :
61 inImage = gdImageCreateFromGif(inFile);
62 break;
63 case IMAGE_PNG :
64 inImage = gdImageCreateFromPng(inFile);
65 break;
66 default :
67 __DCL_ASSERT(false);
68 }
69 fclose(inFile);
70
71 if (!inImage)
72 return false;
73
74 int nWidth;
75 int nHeight;
76 if (gdImageSX(inImage) > nSize || gdImageSY(inImage) > nSize) {
77 double fRate = 1.0;
78 if (gdImageSX(inImage) >= gdImageSY(inImage))
79 fRate = (double)nSize / (double)gdImageSX(inImage);
80 else
81 fRate = (double)nSize / (double)gdImageSY(inImage);
82
83 nWidth = (int)(((double)gdImageSX(inImage)) * fRate);
84 nHeight = (int)(((double)gdImageSY(inImage)) * fRate);
85 }
86 else {
87 nWidth = gdImageSX(inImage);
88 nHeight = gdImageSY(inImage);
89 }
90
91 gdImagePtr outImage = gdImageCreateTrueColor(nWidth, nHeight);
92 if (!outImage) {
93 gdImageDestroy(inImage);
94 return false;
95 }
96
97// gdImageCopyResized(
98 gdImageCopyResampled(
99 outImage, // dst
100 inImage, // src
101 0, // dstX
102 0, // dstY
103 0, // srcX
104 0, // srcY
105 nWidth, // dstW
106 nHeight, // dstH
107 gdImageSX(inImage), // srcW
108 gdImageSY(inImage) // srcH
109 );
110
111 FILE* outFile = fopen(pszThumbnailFile, L"wb");
112 if (!outFile) {
113 gdImageDestroy(inImage);
114 gdImageDestroy(outImage);
115 return false;
116 }
117 gdImageJpeg(outImage, outFile, -1);
118 fclose(outFile);
119
120 gdImageDestroy(inImage);
121 gdImageDestroy(outImage);
122
123 return true;
124}
125
126__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMAGE_GIF
__DCL_BEGIN_NAMESPACE bool GenThumbnailImage(const char *pszSrcFile, const char *pszContentType, const char *pszThumbnailFile, int nSize)
Definition ThumbNail.cpp:15
#define IMAGE_JPEG
#define IMAGE_PNG
#define IMAGE_UNKNOWN