3 if(! class_exists("Photo")) {
14 * supported mimetypes and corresponding file extensions
16 static function supportedTypes() {
18 $t['image/jpeg'] ='jpg';
19 if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
23 public function __construct($data, $type="image/jpeg") {
25 $this->types = $this->supportedTypes();
26 if (!array_key_exists($type,$this->types)){
31 $this->image = @imagecreatefromstring($data);
32 if($this->image !== FALSE) {
33 $this->width = imagesx($this->image);
34 $this->height = imagesy($this->image);
36 imagealphablending($this->image, false);
37 imagesavealpha($this->image, true);
41 public function __destruct() {
43 imagedestroy($this->image);
46 public function is_valid() {
50 public function getWidth() {
54 public function getHeight() {
58 public function getImage() {
62 public function getType() {
65 public function getExt() {
66 return $this->types[$this->type];
69 public function scaleImage($max) {
71 $width = $this->width;
72 $height = $this->height;
74 $dest_width = $dest_height = 0;
76 if((! $width)|| (! $height))
79 if($width > $max && $height > $max) {
80 if($width > $height) {
82 $dest_height = intval(( $height * $max ) / $width);
85 $dest_width = intval(( $width * $max ) / $height);
92 $dest_height = intval(( $height * $max ) / $width);
95 if( $height > $max ) {
96 $dest_width = intval(( $width * $max ) / $height);
100 $dest_width = $width;
101 $dest_height = $height;
107 $dest = imagecreatetruecolor( $dest_width, $dest_height );
108 imagealphablending($dest->image, false);
109 imagesavealpha($dest, true);
110 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
111 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
113 imagedestroy($this->image);
114 $this->image = $dest;
115 $this->width = imagesx($this->image);
116 $this->height = imagesy($this->image);
120 public function rotate($degrees) {
121 $this->image = imagerotate($this->image,$degrees,0);
122 $this->width = imagesx($this->image);
123 $this->height = imagesy($this->image);
128 public function scaleImageUp($min) {
130 $width = $this->width;
131 $height = $this->height;
133 $dest_width = $dest_height = 0;
135 if((! $width)|| (! $height))
138 if($width < $min && $height < $min) {
139 if($width > $height) {
141 $dest_height = intval(( $height * $min ) / $width);
144 $dest_width = intval(( $width * $min ) / $height);
149 if( $width < $min ) {
151 $dest_height = intval(( $height * $min ) / $width);
154 if( $height < $min ) {
155 $dest_width = intval(( $width * $min ) / $height);
159 $dest_width = $width;
160 $dest_height = $height;
166 $dest = imagecreatetruecolor( $dest_width, $dest_height );
167 imagealphablending($dest->image, false);
168 imagesavealpha($dest, true);
169 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
170 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
172 imagedestroy($this->image);
173 $this->image = $dest;
174 $this->width = imagesx($this->image);
175 $this->height = imagesy($this->image);
181 public function scaleImageSquare($dim) {
183 $dest = imagecreatetruecolor( $dim, $dim );
184 imagealphablending($dest->image, false);
185 imagesavealpha($dest, true);
186 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
187 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
189 imagedestroy($this->image);
190 $this->image = $dest;
191 $this->width = imagesx($this->image);
192 $this->height = imagesy($this->image);
196 public function cropImage($max,$x,$y,$w,$h) {
197 $dest = imagecreatetruecolor( $max, $max );
198 imagealphablending($dest->image, false);
199 imagesavealpha($dest, true);
200 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
201 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
203 imagedestroy($this->image);
204 $this->image = $dest;
205 $this->width = imagesx($this->image);
206 $this->height = imagesy($this->image);
209 public function saveImage($path) {
212 $quality = get_config('system','png_quality');
213 if((! $quality) || ($quality > 9))
214 $quality = PNG_QUALITY;
215 imagepng($this->image, $path, $quality);
218 $quality = get_config('system','jpeg_quality');
219 if((! $quality) || ($quality > 100))
220 $quality = JPEG_QUALITY;
221 imagejpeg($this->image,$path,$quality);
226 public function imageString() {
230 $quality = get_config('system','png_quality');
231 if((! $quality) || ($quality > 9))
232 $quality = PNG_QUALITY;
233 imagepng($this->image,NULL, $quality);
236 $quality = get_config('system','jpeg_quality');
237 if((! $quality) || ($quality > 100))
238 $quality = JPEG_QUALITY;
240 imagejpeg($this->image,NULL,$quality);
242 $s = ob_get_contents();
249 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
251 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
255 $guid = $r[0]['guid'];
259 $r = q("INSERT INTO `photo`
260 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
261 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
266 dbesc(datetime_convert()),
267 dbesc(datetime_convert()),
268 dbesc(basename($filename)),
271 intval($this->height),
272 intval($this->width),
273 dbesc($this->imageString()),
292 * Guess image mimetype from filename or from Content-Type header
294 * @arg $filename string Image filename
295 * @arg $fromcurl boolean Check Content-Type header from curl request
297 function guess_image_type($filename, $fromcurl=false) {
302 $h = explode("\n",$a->get_curl_headers());
304 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
307 if (array_key_exists('Content-Type', $headers))
308 $type = $headers['Content-Type'];
311 $ext = pathinfo($filename, PATHINFO_EXTENSION);
312 $types = Photo::supportedTypes();
313 foreach ($types as $m=>$e){
314 if ($ext==$e) return $m;
316 $type = "image/jpeg";
323 function import_profile_photo($photo,$uid,$cid) {
327 $photo_failure = false;
329 $filename = basename($photo);
330 $img_str = fetch_url($photo,true);
332 // guess mimetype from headers or filename
333 $type = guess_image_type($photo,true);
336 $img = new Photo($img_str, $type);
337 if($img->is_valid()) {
339 $img->scaleImageSquare(175);
341 $hash = photo_new_resource();
343 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
346 $photo_failure = true;
348 $img->scaleImage(80);
350 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
353 $photo_failure = true;
355 $img->scaleImage(48);
357 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
360 $photo_failure = true;
364 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
365 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
366 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
369 $photo_failure = true;
372 $photo = $a->get_baseurl() . '/images/person-175.jpg';
373 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
374 $micro = $a->get_baseurl() . '/images/person-48.jpg';
377 return(array($photo,$thumb,$micro));