aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/send2kindle/images.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/3rdparty/libraries/send2kindle/images.php')
-rw-r--r--inc/3rdparty/libraries/send2kindle/images.php136
1 files changed, 136 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/send2kindle/images.php b/inc/3rdparty/libraries/send2kindle/images.php
new file mode 100644
index 00000000..63f0562a
--- /dev/null
+++ b/inc/3rdparty/libraries/send2kindle/images.php
@@ -0,0 +1,136 @@
1<?php
2
3/**
4 * Get images from content and prepare to save in articles
5 */
6class Images {
7
8 /**
9 * JPER quality for resize
10 */
11 const JPEG_QUALITY = 80;
12
13 /**
14 * Articles content
15 * @var string
16 */
17 private $_content;
18
19 /**
20 * Storage to keep images
21 * @var object Storage
22 */
23 private $_storage;
24
25 /**
26 * images from content
27 * @var array
28 */
29 private $_images_from_content = array();
30
31 /**
32 * Prepare get images
33 * @param Strage $storage
34 * @param string $article_content
35 */
36 public function __construct(Storage $storage, $article_content)
37 {
38 $this->_storage = $storage;
39 $this->_content = $article_content;
40 $this->_images_from_content = $this->_get_images_from_content($article_content);
41 }
42
43 /**
44 * get images from url
45 * @param string $content
46 * @return array images hashtable
47 */
48 private function _get_images_from_content($content)
49 {
50 $result = array();
51 preg_match_all('/src=\"([a-zA-Z0-9\.\/\-\_\?\+\%\~\&\;\=\:]+)\"/i', $content, $result);
52
53 return $result[1];
54 }
55
56 /**
57 * Start conversion
58 * @return string converted content
59 */
60 public function convert()
61 {
62 foreach ( $this->_images_from_content as $n => $image_url )
63 {
64 $image = $this->_get_image($image_url);
65 $this->_content = str_replace($image_url, '" recindex="'.(int)basename($image), $this->_content);
66 }
67
68 return $this->_content;
69 }
70
71 /**
72 * Resize image
73 * @param string $file path
74 * @param int $new_width max width
75 */
76 private function _resize($file, $new_width = 500)
77 {
78 list($width, $height) = getimagesize($file);
79
80 $new_height = 0;
81
82 //setup the new size of the image
83 if( $width > $new_width )
84 {
85 $ratio = $height/$width;
86 $new_height = $new_width * $ratio;
87 }
88 else
89 {
90 $new_width = $width;
91 $new_height = $height;
92 }
93
94 // resample the image
95 $new_image = imagecreatetruecolor($new_width, $new_height);
96
97 $type = exif_imagetype ( $file );
98
99 switch ( $type )
100 {
101 case IMAGETYPE_JPEG:
102 $old_image = imagecreatefromjpeg($file);
103 imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
104 imagejpeg($new_image, $file, self::JPEG_QUALITY);
105 break;
106 case IMAGETYPE_PNG:
107 $old_image = imagecreatefrompng($file);
108 imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
109 imagepng($new_image, $file);
110 break;
111 case IMAGETYPE_GIF:
112 $old_image = imagecreatefromgif($file);
113 imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
114 imagegif($new_image, $file);
115 break;
116 }
117 }
118
119 /**
120 * Resize image
121 * @return string image path
122 */
123 private function _get_image($url)
124 {
125 $image_data = @file_get_contents($url);
126
127 if ( $image_data !== false )
128 {
129 $image_name = $this->_storage->save_image($image_data);
130 $this->_resize($image_name);
131
132 return $image_name;
133 }
134 }
135
136} \ No newline at end of file