public static function move_uploaded_file($filename, $destination) { $r = move_uploaded_file($filename, $destination); PerchUtil::set_file_permissions($destination); return $r; }
public function resize_image($image_path, $target_w = false, $target_h = false, $crop = false, $suffix = false) { $Perch = Perch::fetch(); $bail = false; if ($this->mode === false) { return false; } if ($crop) { PerchUtil::debug('Resizing and cropping image... (' . $this->mode . ', ' . sprintf('w%d h%d @%dx %s', $target_w, $target_h, $this->density, $suffix) . ')'); } else { PerchUtil::debug('Resizing image... (' . $this->mode . ', ' . sprintf('w%d h%d @%dx %s', $target_w, $target_h, $this->density, $suffix) . ')'); } $info = getimagesize($image_path); // WebP? if (!is_array($info)) { if ($this->is_webp($image_path)) { $info = $this->get_webp_size($image_path); } } // SVG? $svg = false; if (!is_array($info)) { // $svg gets populated with the mime type if it's an SVG $svg = $this->is_svg($image_path); if ($svg) { $info = $this->get_svg_size($image_path); // Can't crop SVG $crop = false; } } if (!is_array($info)) { return false; } if ($svg) { // Only need one SVG file for all sizes. $save_as = $image_path; } else { $save_as = $this->get_resized_filename($image_path, $target_w, $target_h, $suffix, $this->density); } $image_w = $info[0]; $image_h = $info[1]; $crop_x = 0; $crop_y = 0; $crop_w = 0; $crop_h = 0; $image_ratio = $image_w / $image_h; if ($svg) { // Constrain by width if ($target_w) { $new_w = $target_w; $new_h = $target_w / $image_ratio; } // Constrain by height if ($target_h) { $new_h = $target_h; $new_w = $target_h * $image_ratio; } } else { // Constrain by width if ($target_w && $image_w >= $target_w) { $new_w = $target_w; $new_h = $target_w / $image_ratio; } // Constrain by height if ($target_h && $image_h >= $target_h) { $new_h = $target_h; $new_w = $target_h * $image_ratio; } } // Both specified, and crop set if ($target_w && $target_h && $crop) { $crop_w = $target_w; $crop_h = $target_h; $crop_ratio = $crop_w / $crop_h; if ($image_ratio >= $crop_ratio) { // Landscape or square crop $new_h = (int) $target_h; $new_w = $target_h * $image_ratio; $crop_y = 0; $crop_x = $new_w / 2 - $target_w / 2; } if ($crop_ratio > $image_ratio) { // Portrait crop $new_w = (int) $target_w; $new_h = $target_w / $image_ratio; $crop_x = 0; $crop_y = $new_h / 2 - $target_h / 2; } // Check we're not cropping upwardly if ($crop_w > $image_w || $crop_h > $image_h) { $crop_x = 0; $crop_y = 0; $crop_w = 0; $crop_h = 0; $crop = false; } //PerchUtil::debug("Crop info: $crop_x, $crop_y, $crop_w, $crop_h"); } if ($target_w && $target_h && !$crop) { // Normal resize if ($this->box_constrain) { if ($image_w / $target_w > $image_h / $target_h) { $new_w = $target_w; $new_h = $target_w / $image_ratio; } else { $new_h = $target_h; $new_w = $target_h * $image_ratio; } } else { if ($image_w > $image_h) { $new_w = $target_w; $new_h = $target_w / $image_ratio; } if ($image_h > $image_w) { $new_h = $target_h; $new_w = $target_h * $image_ratio; } } } // Default if (!isset($new_w)) { $new_w = $image_w; $new_h = $image_h; } // Prepare returned array $out = array(); $out['w'] = (int) $new_w; $out['h'] = (int) $new_h; $out['file_path'] = $save_as; $parts = explode(DIRECTORY_SEPARATOR, $save_as); $out['file_name'] = array_pop($parts); $out['web_path'] = str_replace(PERCH_RESFILEPATH . DIRECTORY_SEPARATOR, PERCH_RESPATH . '/', $save_as); $out['density'] = $this->density; // If SVG, we can return at this point. if ($svg) { $out['mime'] = $svg; return $out; } if ($crop) { if ($crop_w) { $out['w'] = (int) $crop_w; } if ($crop_h) { $out['h'] = (int) $crop_h; } } // Check we're not upsizing if ($crop) { if ($crop_w > $image_w || $crop_h > $image_h) { $bail = true; } } else { if ($new_w > $image_w || $new_h > $image_h) { $bail = true; } } // Check we're not resizing to the same exact size, as this just kills quality if ($crop) { if ($crop_w == $image_w && $crop_h == $image_h) { $bail = true; } } else { if ($new_w == $image_w && $new_h == $image_h) { $bail = true; } } // Bail? if ($bail) { copy($image_path, $save_as); PerchUtil::set_file_permissions($save_as); // reset sizes $out['w'] = (int) $image_w; $out['h'] = (int) $image_h; $Perch->event('assets.create_image', new PerchAssetFile($out)); return $out; } // Density $new_w = floor($new_w * $this->density); $new_h = floor($new_h * $this->density); $crop_w = floor($crop_w * $this->density); $crop_h = floor($crop_h * $this->density); $crop_x = floor($crop_x * $this->density); $crop_y = floor($crop_y * $this->density); //PerchUtil::debug('Density: '.$this->density); $r = false; if ($this->mode == 'gd') { $r = $this->resize_with_gd($image_path, $save_as, $new_w, $new_h, $crop_w, $crop_h, $crop_x, $crop_y); } if ($this->mode == 'imagick') { $r = $this->resize_with_imagick($image_path, $save_as, $new_w, $new_h, $crop_w, $crop_h, $crop_x, $crop_y); } if ($r) { $out['mime'] = $r; } PerchUtil::set_file_permissions($save_as); $Perch->event('assets.create_image', new PerchAssetFile($out)); if ($r) { return $out; } return false; }
} } else { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } fclose($in); fclose($out); } else { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } } // If it's the last chunk if ($chunks == 0 || $chunk == $chunks - 1) { $newFileName = str_replace('_uploading_', '', $fileName); rename($targetDir . DIRECTORY_SEPARATOR . $fileName, $targetDir . DIRECTORY_SEPARATOR . $newFileName); $albumID = (int) $_REQUEST["albumID"]; $API = new PerchAPI(1.0, 'perch_gallery'); $Images = new PerchGallery_Images($API); $Template = $API->get('Template'); $Template->set('gallery/image.html', 'gallery'); $data = array(); $data['imageAlt'] = PerchUtil::strip_file_extension($newFileName); $data['albumID'] = $albumID; $Image = $Images->create($data); if (is_object($Image)) { $Image->process_versions($newFileName, $Template); } PerchUtil::set_file_permissions($targetDir . DIRECTORY_SEPARATOR . $newFileName); //file_put_contents(__DIR__.'/log.txt', strip_tags(PerchUtil::output_debug(true))); } // Return JSON-RPC response die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');