function SaveHeaderImage() { global $page, $dataDir, $dirPrefix, $langmessage; includeFile('tool/Images.php'); $page->ajaxReplace = array(); //source file $source_file_rel = $_REQUEST['file']; if (!empty($_REQUEST['src'])) { $source_file_rel = rawurldecode($_REQUEST['src']); if (!empty($dirPrefix)) { $len = strlen($dirPrefix); $source_file_rel = substr($source_file_rel, $len); } } $source_file_rel = '/' . ltrim($source_file_rel, '/'); $source_file_full = $dataDir . $source_file_rel; if (!file_exists($source_file_full)) { message($langmessage['OOPS'] . ' (Source file not found)'); return; } $src_img = thumbnail::getSrcImg($source_file_full); if (!$src_img) { message($langmessage['OOPS'] . ' (Couldn\'t create image [1])'); return; } //size and position variables $orig_w = $width = imagesx($src_img); $orig_h = $height = imagesy($src_img); $posx = $posy = 0; if (isset($_REQUEST['posx']) && is_numeric($_REQUEST['posx'])) { $posx = $_REQUEST['posx']; } if (isset($_REQUEST['posy']) && is_numeric($_REQUEST['posy'])) { $posy = $_REQUEST['posy']; } if (isset($_REQUEST['width']) && is_numeric($_REQUEST['width'])) { $width = $_REQUEST['width']; } if (isset($_REQUEST['height']) && is_numeric($_REQUEST['height'])) { $height = $_REQUEST['height']; } //check to see if the image needs to be resized if ($posx == 0 && $posy == 0 && $width == $orig_w && $height == $orig_h) { $this->SetImage($source_file_rel, $width, $height); return; } //destination file $name = basename($source_file_rel); $parts = explode('.', $name); $type = array_pop($parts); if (count($parts) > 1) { $time_part = array_pop($parts); if (!ctype_digit($time_part)) { $parts[] = $time_part; } } $name = implode('.', $parts); $time = time(); if (isset($_REQUEST['time']) && ctype_digit($_REQUEST['time'])) { $time = $_REQUEST['time']; } //$dest_img_rel = '/data/_uploaded/headers/'.$name.'.'.$time.'.'.$type; $dest_img_rel = '/data/_uploaded/headers/' . $name . '.' . $time . '.png'; $dest_img_full = $dataDir . $dest_img_rel; //make sure the folder exists if (!gpFiles::CheckDir(dirname($dest_img_full))) { message($langmessage['OOPS'] . ' (Couldn\'t create directory)'); return false; } if (!thumbnail::createImg($src_img, $dest_img_full, $posx, $posy, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h, $width, $height)) { message($langmessage['OOPS'] . ' (Couldn\'t create image [2])'); return; } if ($this->SetImage($dest_img_rel, $width, $height)) { includeFile('admin/admin_uploaded.php'); admin_uploaded::CreateThumbnail($dest_img_full); } }
/** * Create a resized image of the file at $src_relative * */ static function CreateImage($src_relative, $width, $height) { global $dataDir; $src_path = $dataDir . '/data/_uploaded' . $src_relative; if (!file_exists($src_path)) { return false; } //compare to actual size includeFile('tool/Images.php'); $src_img = thumbnail::getSrcImg($src_path); if (!$src_img) { return false; } //Original Size $actual_w = imagesx($src_img); $actual_h = imagesy($src_img); if ($actual_w <= $width && $actual_h <= $height) { return false; } $info = gp_resized::ImageInfo($src_relative, $width, $height); if (!$info) { return false; } $dest_index = $info['index']; if (!$dest_index) { $dest_index = gp_resized::NewIndex(); } $dest_path = $dataDir . '/data/_resized/' . $dest_index . '/' . $info['name']; $exists_before = file_exists($dest_path); //make sure the folder exists if (!gpFiles::CheckDir(common::DirName($dest_path))) { return false; } //create new resized image if (!thumbnail::createImg($src_img, $dest_path, 0, 0, 0, 0, $width, $height, $actual_w, $actual_h)) { return false; } //not needed if the resized image is larger than the original if (filesize($dest_path) > filesize($src_path)) { if (!$exists_before) { unlink($dest_path); } return false; } $data['index'] = $dest_index; $data['w'] = $width; $data['h'] = $height; $data['img'] = $src_relative; return $data; }
/** * @deprecated */ function maxArea($source_path, $dest_path, $max_area = 1024000) { $src_img = thumbnail::getSrcImg($source_path, $dest_path); //memory usage before and after this call are small 1093804 vs 1094616 if (!$src_img) { return false; } //Original Size $old_x = imagesx($src_img); $old_y = imagesy($src_img); $old_area = $old_x * $old_y; //don't enlarge check 1 if ($old_area < $max_area) { return move_uploaded_file($source_path, $dest_path); } //Calculate the new size $inv_ratio = $old_y / $old_x; $new_y = sqrt($max_area * $inv_ratio); $new_y = round($new_y); $new_x = round($max_area / $new_y); //don't enlarge check 2 $new_area = $new_y * $new_x; if ($new_area > $old_area) { return move_uploaded_file($source_path, $dest_path); } return thumbnail::createImg($src_img, $dest_path, 0, 0, 0, 0, $new_x, $new_y, $old_x, $old_y); }
/** * Create a rectangular image at $dest_path from an image at $source_path * * @param string $source_path location of the source image * @param string $dest_path location of the image to be created * @param int $new_w The width of the new image * @param int $new_h The height of the new image * @param string $img_type A string representing the type of the source file (png, jpg) * @return bool */ static function CreateRect($source_path, $dest_path, $new_w = 50, $new_h = 50, $img_type = false) { $src_img = thumbnail::getSrcImg($source_path, $img_type); if (!$src_img) { return false; } //Size $old_w = imagesx($src_img); $old_h = imagesy($src_img); $dst_x = 0; $dst_y = 0; $width_ratio = $new_w / $old_w; $height_ratio = $new_h / $old_h; if ($width_ratio < $height_ratio) { $temp_h = round($width_ratio * $old_h); $temp_w = $new_w; } else { $temp_w = round($height_ratio * $old_w); $temp_h = $new_h; } return thumbnail::createImg($src_img, $dest_path, $dst_x, $dst_y, 0, 0, $temp_w, $temp_h, $old_w, $old_h, $new_w, $new_h); }