public static function resize_img($root_path, $root_url, $output_path, $max_x, $max_y, $picture, $alternate = NULL, $overwrite = FALSE, $resize_type = RESIZE_CROP) { $final_path = NULL; if ($alternate) { if (preg_match("|^http://|", $alternate)) { throw new CNException(BAD_PARAMETER, "Alternate image passed to resizing functions must not be a URL"); } if (!preg_match("#^(files|Themes|images)/#", $alternate)) { throw new CNException(BAD_PARAMETER, "Alternate image passed to resizing functions must be relative to the web directory; {$alternate} is not valid"); } } if ($picture instanceof StoredFile) { $stored_file = $picture; $pic_path = $picture->filename; } else { if (defined("NEW_STORAGE")) { // check for broken or deprecated calling code if (preg_match("|^files/files|", $picture)) { throw new CNException(INVALID_ID, "Broken image ID - starting with files/files!"); } if (preg_match("|^files/pa://|", $picture)) { throw new CNException(INVALID_ID, "Broken image ID - check for code adding 'files/' to the start of a pa:// image URL"); } } $stored_file = NULL; $image_path = NULL; if (getimagesize(PA::$project_dir . "/{$root_path}/{$picture}")) { $image_path = PA::$project_dir . "/{$root_path}"; } else { if (getimagesize(PA::$core_dir . "/{$root_path}/{$picture}")) { $image_path = PA::$core_dir . "/{$root_path}"; } else { if (getimagesize(PA::$project_dir . "/{$root_path}/{$alternate}")) { $image_path = PA::$project_dir . "/{$root_path}"; } else { if (getimagesize(PA::$core_dir . "/{$root_path}/{$alternate}")) { $image_path = PA::$core_dir . "/{$root_path}"; } } } } if ($picture && is_file("{$image_path}/{$picture}") && getimagesize("{$image_path}/{$picture}") !== false) { $pic_path = $picture; } else { if (!$alternate || !is_file("{$image_path}/{$alternate}")) { // we could throw a FILE_NOT_FOUND exception here, but that // breaks things, so instead we output an image tag with the // requested size that refers to the original path. this // way the admin will see 404 errors in the log, and maybe // fix what's wrong. $final_path = $picture; $width = $max_x; $height = $max_y; } else { $pic_path = $alternate; } } } if (!$final_path) { // if it's a png or gif, convert to png - so we don't lose transparency. otherwise jpg. $path_parts = pathinfo($pic_path); $ext = strtolower($path_parts['extension']); switch ($ext) { case 'png': case 'gif': $ext = 'png'; $mime_type = "image/png"; break; default: $ext = 'jpg'; $mime_type = "image/jpeg"; break; } $prefix = ImageResize::$resize_type_prefixes[$resize_type]; if (!$prefix) { throw new CNException(BAD_PARAMETER, "Invalid resize type: {$resize_type}"); } // 'dim' string for file link $file_link_dim = $prefix . "-" . $max_x . "x" . $max_y; if ($stored_file) { // have we resized this already? $link = Storage::find_thumb($stored_file->file_id, $file_link_dim); if ($link) { $thumb_id = $link['file_id']; } else { // nope - we have to resize it now $picture_full_path = $stored_file->getPath(); // temp output filename $resized_fn_tmp = tempnam(ini_get("upload_tmp_dir"), "rsz"); $resized_fn = $resized_fn_tmp . "." . $ext; rename($resized_fn_tmp, $resized_fn); // leaf name, to show to users later on $leaf = $stored_file->filename; Logger::log("Resizing image '{$picture_full_path}' from Storage into {$resized_fn}", LOGGER_ACTION); ImageResize::do_resize_to_max_side($picture_full_path, $resized_fn, $max_x, $max_y, $resize_type); list($w, $h) = getimagesize($resized_fn); // make the new file $thumb_id = Storage::save($resized_fn, $file_link_dim . "-" . $leaf, "throwaway", $mime_type, array("width" => $w, "height" => $h)); unlink($resized_fn); // link it to the original so we can find it again Storage::link($thumb_id, array("role" => "thumb", "dim" => $file_link_dim, "file" => $stored_file->file_id)); } // and return the details $thumb = Storage::get($thumb_id); return array('url' => $thumb->getURL(), 'width' => $thumb->width, 'height' => $thumb->height, 'size_attr' => 'width="' . $thumb->width . '" height="' . $thumb->height . '"'); } else { // relative path to resized file $resized_pic_path = $prefix . "_" . $max_x . "x" . $max_y . "/" . preg_replace("/\\.[A-Za-z]+\$/", "", $pic_path) . ".{$ext}"; // abs path to resized file $resized_fn = PA::$project_dir . "/{$root_path}/{$output_path}/{$resized_pic_path}"; // only overwrite an existing file if it's out of date or we have been told to (via $overwrite) if (!file_exists($resized_fn) || filemtime($resized_fn) < filemtime("{$image_path}/{$pic_path}") || $overwrite) { // make all path parts up to the image if (!is_dir(dirname($resized_fn))) { $mkdir_path = PA::$project_dir . "/{$root_path}/{$output_path}"; ImageResize::try_mkdir($mkdir_path); foreach (explode("/", dirname($resized_pic_path)) as $path_part) { $mkdir_path .= "/{$path_part}"; ImageResize::try_mkdir($mkdir_path); } } ImageResize::do_resize_to_max_side("{$image_path}/{$pic_path}", $resized_fn, $max_x, $max_y, $resize_type); clearstatcache(); } } list($width, $height) = getimagesize($resized_fn); $final_path = "{$output_path}/" . dirname($resized_pic_path) . "/" . rawurlencode(basename($resized_pic_path)); } return array('final_path' => $final_path, 'width' => $width, 'height' => $height, 'size_attr' => 'width="' . $width . '" height="' . $height . '"'); }
public static function resize_img($root_path, $root_url, $output_path, $max_x, $max_y, $picture, $alternate = NULL, $overwrite = FALSE, $resize_type = RESIZE_CROP) { $final_path = NULL; if ($picture && is_file("{$root_path}/{$picture}") && getimagesize("{$root_path}/{$picture}") !== false) { // $sz = ; var_dump($sz); $pic_path = $picture; } else { if (!$alternate || !is_file("{$root_path}/{$alternate}")) { // we could throw a FILE_NOT_FOUND exception here, but that // breaks things, so instead we output an image tag with the // requested size that refers to the original path. this // way the admin will see 404 errors in the log, and maybe // fix what's wrong. $final_path = $picture; $width = $max_x; $height = $max_y; } else { $pic_path = $alternate; } } //print_r($pic_path);echo 'Final path';die; if (!$final_path) { // if it's a png or gif, convert to png - so we don't lose transparency. otherwise jpg. $path_parts = pathinfo($pic_path); $ext = strtolower($path_parts['extension']); switch ($ext) { case 'png': case 'gif': $ext = 'png'; break; default: $ext = 'jpg'; break; } $prefix = ImageResize::$resize_type_prefixes[$resize_type]; if (!$prefix) { throw new PAException(BAD_PARAMETER, "Invalid resize type: {$resize_type}"); } // relative path to resized file $resized_pic_path = $prefix . "_" . $max_x . "x" . $max_y . "/" . preg_replace("/\\.[A-Za-z]+\$/", "", $pic_path) . ".{$ext}"; // abs path to resized file $resized_fn = "{$root_path}/{$output_path}/{$resized_pic_path}"; // only overwrite an existing file if it's out of date or we have been told to (via $overwrite) if (!file_exists($resized_fn) || filemtime($resized_fn) < filemtime("{$root_path}/{$pic_path}") || $overwrite) { // make all path parts up to the image if (!is_dir(dirname($resized_fn))) { $mkdir_path = "{$root_path}/{$output_path}"; ImageResize::try_mkdir($mkdir_path); foreach (explode("/", dirname($resized_pic_path)) as $path_part) { $mkdir_path .= "/{$path_part}"; ImageResize::try_mkdir($mkdir_path); } } ImageResize::do_resize_to_max_side("{$root_path}/{$pic_path}", $resized_fn, $max_x, $max_y, $resize_type); clearstatcache(); } list($width, $height) = @getimagesize($resized_fn); $final_path = "{$output_path}/" . dirname($resized_pic_path) . "/" . rawurlencode(basename($resized_pic_path)); } return array('final_path' => $final_path, 'width' => $width, 'height' => $height, 'size_attr' => 'width="' . $width . '" height="' . $height . '"'); }