3 if(! class_exists("Photo")) {
11 public function __construct($data) {
13 $this->image = @imagecreatefromstring($data);
14 if($this->image !== FALSE) {
15 $this->width = imagesx($this->image);
16 $this->height = imagesy($this->image);
21 public function __destruct() {
23 imagedestroy($this->image);
26 public function is_valid() {
30 public function getWidth() {
34 public function getHeight() {
38 public function getImage() {
42 public function scaleImage($max) {
44 $width = $this->width;
45 $height = $this->height;
47 $dest_width = $dest_height = 0;
49 if((! $width)|| (! $height))
52 if($width > $max && $height > $max) {
53 if($width > $height) {
55 $dest_height = intval(( $height * $max ) / $width);
58 $dest_width = intval(( $width * $max ) / $height);
65 $dest_height = intval(( $height * $max ) / $width);
68 if( $height > $max ) {
69 $dest_width = intval(( $width * $max ) / $height);
74 $dest_height = $height;
80 $dest = imagecreatetruecolor( $dest_width, $dest_height );
81 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
83 imagedestroy($this->image);
85 $this->width = imagesx($this->image);
86 $this->height = imagesy($this->image);
92 public function scaleImageUp($min) {
94 $width = $this->width;
95 $height = $this->height;
97 $dest_width = $dest_height = 0;
99 if((! $width)|| (! $height))
102 if($width < $min && $height < $min) {
103 if($width > $height) {
105 $dest_height = intval(( $height * $min ) / $width);
108 $dest_width = intval(( $width * $min ) / $height);
113 if( $width < $min ) {
115 $dest_height = intval(( $height * $min ) / $width);
118 if( $height < $min ) {
119 $dest_width = intval(( $width * $min ) / $height);
123 $dest_width = $width;
124 $dest_height = $height;
130 $dest = imagecreatetruecolor( $dest_width, $dest_height );
131 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
133 imagedestroy($this->image);
134 $this->image = $dest;
135 $this->width = imagesx($this->image);
136 $this->height = imagesy($this->image);
142 public function scaleImageSquare($dim) {
144 $dest = imagecreatetruecolor( $dim, $dim );
145 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
147 imagedestroy($this->image);
148 $this->image = $dest;
149 $this->width = imagesx($this->image);
150 $this->height = imagesy($this->image);
154 public function cropImage($max,$x,$y,$w,$h) {
155 $dest = imagecreatetruecolor( $max, $max );
156 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
158 imagedestroy($this->image);
159 $this->image = $dest;
160 $this->width = imagesx($this->image);
161 $this->height = imagesy($this->image);
164 public function saveImage($path) {
165 imagejpeg($this->image,$path,100);
168 public function imageString() {
170 imagejpeg($this->image,NULL,100);
171 $s = ob_get_contents();
178 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
180 $r = q("INSERT INTO `photo`
181 ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
182 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
186 dbesc(datetime_convert()),
187 dbesc(datetime_convert()),
188 dbesc(basename($filename)),
190 intval($this->height),
191 intval($this->width),
192 dbesc($this->imageString()),
210 function import_profile_photo($photo,$uid,$cid) {
214 $photo_failure = false;
216 $filename = basename($photo);
217 $img_str = fetch_url($photo,true);
218 $img = new Photo($img_str);
219 if($img->is_valid()) {
221 $img->scaleImageSquare(175);
223 $hash = photo_new_resource();
225 $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 4 );
228 $photo_failure = true;
230 $img->scaleImage(80);
232 $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 5 );
235 $photo_failure = true;
237 $img->scaleImage(48);
239 $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 6 );
242 $photo_failure = true;
246 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
247 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
248 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.jpg';
251 $photo_failure = true;
254 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
255 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
256 $micro = $a->get_baseurl() . '/images/default-profile-mm.jpg';
259 return(array($photo,$thumb,$micro));