/** * creates a thumbnail and put it in the THUMBS dir, or print it. * * @param string $src reference to the original file * @param int $maxY max height of thumb * @param int $maxX max width of thumb * @return true if success, false if error */ static function createThumbnail($src, $filename, $targetDir, $writeToFile = true, $maxY = false, $maxX = false) { if (!$maxY && !$maxX) { return false; } $file = open_image($src); if ($file) { $oldX = imagesx($file); $oldY = imagesy($file); $x = 0; $y = 0; $assumedY = $maxX * ($oldY / $oldX); $assumedX = $maxY * ($oldX / $oldY); if ($assumedX > $maxX) { $x = $maxX; $y = $assumedY; } if ($assumedY > $maxY) { $x = $assumedX; $y = $maxY; } $img = imagecreatetruecolor($x, $y); imagecopyresampled($img, $file, 0, 0, 0, 0, $x, $y, $oldX, $oldY); $destination = $targetDir . '/' . $filename; if ($writeToFile) { imagepng($img, $destination, 40); } else { header('content-type: image/png'); imagepng($img); } } return true; }
public function process() { $this->parseInput(); $this->basicCheck(); $this->purgeExif(); if ($this->inputDim[0] === $this->targetDim[0] && $this->inputDim[1] === $this->targetDim[1]) { if ($this->inputFileSize < $this->targetFileSize) { return; } $image = open_image($this->inputPath, $this->inputDim); } else { $inputImage = open_image($this->inputPath, $this->inputDim); $start = [0, 0]; $inDim = [$this->inputDim[0], $this->inputDim[1]]; $outDim = [$this->targetDim[0], $this->targetDim[1]]; // figure out how to crop. if ($this->inputDim[0] / $this->inputDim[1] >= $this->targetDim[0] / $this->targetDim[1]) { $inDim[0] = $this->targetDim[0] / $this->targetDim[1] * $this->inputDim[1]; $start[0] = ($this->inputDim[0] - $inDim[0]) / 2; } else { $inDim[1] = $this->targetDim[1] / $this->targetDim[0] * $this->inputDim[0]; $start[1] = ($this->inputDim[1] - $inDim[1]) / 2; } // don't scale if input image is smaller. if ($inDim[0] < $outDim[0] || $inDim[1] < $outDim[1]) { $outDim = $inDim; } $image = imagecreatetruecolor($outDim[0], $outDim[1]); imagecopyresampled($image, $inputImage, 0, 0, $start[0], $start[1], $outDim[0], $outDim[1], $inDim[0], $inDim[1]); } imagejpeg($image, $this->inputPath); $this->parseInput(true); }
$new_height = 100; } // get mime type of src $mime_type = mime_type($src); // check to see if this image is in the cache already check_cache($mime_type); // if not in cache then clear some space and generate a new file cleanCache(); ini_set('memory_limit', '50M'); // make sure that the src is gif/jpg/png if (!valid_src_mime_type($mime_type)) { displayError('Invalid src mime type: ' . $mime_type); } if (strlen($src) && file_exists($src)) { // open the existing image $image = open_image($mime_type, $src); if ($image === false) { displayError('Unable to open image : ' . $src); } // Get original width and height $width = imagesx($image); $height = imagesy($image); // generate new w/h if not provided if ($new_width && !$new_height) { $new_height = $height * ($new_width / $width); } elseif ($new_height && !$new_width) { $new_width = $width * ($new_height / $height); } elseif (!$new_width && !$new_height) { $new_width = $width; $new_height = $height; }
function k_resize_image($src, $dest = 0, $new_width = 0, $new_height = 0, $zoom_crop = 1, $enforce_max = 0, $quality = 80, $crop_position = 'middle', $check_thumb_exists = 0) { global $FUNCS; // check to see if GD function exist if (!function_exists('imagecreatetruecolor')) { return displayError('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library'); } if (trim($src) == '') { return displayError('Source image not set'); } // get mime type of src $mime_type = mime_type($src); ini_set('memory_limit', "50M"); // make sure that the src is gif/jpg/png if (!valid_src_mime_type($mime_type)) { return displayError("Invalid src mime type: " . $mime_type); } if (strlen($src) && file_exists($src)) { // open the existing image $image = open_image($mime_type, $src); if ($image === false) { return displayError('Unable to open image : ' . $src); } // Get original width and height $width = imagesx($image); $height = imagesy($image); // generate new w/h if not provided if ($new_width && !$new_height) { $new_height = $height * ($new_width / $width); } elseif ($new_height && !$new_width) { $new_width = $width * ($new_height / $height); } elseif (!$new_width && !$new_height) { $new_width = $width; $new_height = $height; } // If new dimensions cannot exceed certain values if ($enforce_max) { // the supplied width and height were actually the max permissible values. $max_width = $new_width; $max_height = $new_height; // make the new values the same as that of the source image $new_width = $width; $new_height = $height; // if new dimensions already within bounds (and this not a thumbnail that we are creating), return. if ($dest && $new_width <= $max_width && $new_height <= $max_height) { return; } if ($new_width > $max_width) { if (!$zoom_crop) { $ratio = (double) ($max_width / $new_width); $new_width = (int) ($new_width * $ratio); $new_height = (int) ($new_height * $ratio); } else { $new_width = $max_width; } } // if new height still overshoots maximum value if ($new_height > $max_height) { if (!$zoom_crop) { $ratio = (double) ($max_height / $new_height); $new_width = (int) ($new_width * $ratio); $new_height = (int) ($new_height * $ratio); } else { $new_height = $max_height; } } } // Create filename if not provided one (happens only for thumbnails) if (!$dest) { $path_parts = $FUNCS->pathinfo($src); $thumb_name = $path_parts['filename'] . '-' . round($new_width) . 'x' . round($new_height) . '.' . $path_parts['extension']; $thumbnail = $path_parts['dirname'] . '/' . $thumb_name; if ($check_thumb_exists && file_exists($thumbnail)) { return $thumb_name; } } // create a new true color image $canvas = imagecreatetruecolor($new_width, $new_height); imagealphablending($canvas, false); // Create a new transparent color for image $color = imagecolorallocatealpha($canvas, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($canvas, 0, 0, $color); // Restore transparency blending imagesavealpha($canvas, true); if ($zoom_crop) { $src_x = $src_y = 0; $src_w = $width; $src_h = $height; $cmp_x = $width / $new_width; $cmp_y = $height / $new_height; // if new dimensions equal to the original (and this not a thumbnail that we are creating), return. if ($dest && $cmp_x == 1 && $cmp_y == 1) { return; } // calculate x or y coordinate and width or height of source if ($cmp_x > $cmp_y) { $src_w = round($width / $cmp_x * $cmp_y); $src_x = round(($width - $width / $cmp_x * $cmp_y) / 2); } elseif ($cmp_y > $cmp_x) { $src_h = round($height / $cmp_y * $cmp_x); $src_y = round(($height - $height / $cmp_y * $cmp_x) / 2); } switch ($crop_position) { case 'top_left': $src_x = 0; $src_y = 0; break; case 'top_center': $src_y = 0; break; case 'top_right': $src_x *= 2; $src_y = 0; break; case 'middle_left': $src_x = 0; break; case 'middle': break; case 'middle_right': $src_x *= 2; break; case 'bottom_left': $src_x = 0; $src_y *= 2; break; case 'bottom_center': $src_y *= 2; break; case 'bottom_right': $src_x *= 2; $src_y *= 2; break; } imagecopyresampled($canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h); } else { // copy and resize part of an image with resampling imagecopyresampled($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } if (!$dest) { $dest = $thumbnail; } // output image to browser based on mime type save_image($mime_type, $canvas, $dest, $quality); // remove image from memory imagedestroy($canvas); return $thumb_name; } else { if (strlen($src)) { return displayError("image " . $src . " not found"); } else { return displayError("no source specified"); } } return; }
function resizeImage($fileAndPath, $width = 0, $height = 0) { $image = open_image($fileAndPath); if ($image === false) { die(throwError("Unable to open image for resizing")); } $height = imagesy($image); $width = imagesx($image); // we are resizing proportionally to a max width if ($width > 0 && $maxHeight == 0) { $ratio = $height / $width; $new_width = $maxWidth; $new_height = $new_width * $ratio; //die("new height: $new_height"); } if ($width == 0 && $maxHeight > 0) { // we are resizing proportionally to a max height $ratio = $width / $height; $new_height = $maxHeight; $new_width = $new_height * $ratio; } if ($width > 0 && $maxHeight > 0) { // we are resizing to absolute proportions in both directions $new_height = $maxHeight; $new_width = $maxWidth; } $image_resized = imagecreatetruecolor($new_width, $new_height) or die(throwError("“imagecreatetruecolor” has failed. Check that the GD library is active.")); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); saveImageInFormat($image_resized, $fileAndPath, "jpg"); }
public function fix($path) { $dim = getimagesize($path); $fileSize = filesize($path); if ($fileSize > $this->hardMaxFileSize) { $this->errors = [trans('users.show.edit.cover.upload.too_large')]; return false; } elseif ($dim === false || !in_array($dim[2], [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG], true)) { $this->errors = [trans('users.show.edit.cover.upload.unsupported_format')]; return false; } elseif ($dim[0] > $this->hardMaxDim[0] || $dim[1] > $this->hardMaxDim[1]) { $this->errors = [trans('users.show.edit.cover.upload.too_large')]; return false; } if ($dim[2] === IMAGETYPE_JPEG) { exec("jhead -autorot -purejpg -q " . escapeshellarg($path)); $dim = getimagesize($path); } $maxDim = [1800, 500]; $maxFileSize = 1000000; if ($dim[0] === $maxDim[0] && $dim[1] === $maxDim[1]) { if (filesize($path) < $maxFileSize) { return; } $image = open_image($path, $dim); } else { $inputImage = open_image($path, $dim); $start = [0, 0]; $inDim = [$dim[0], $dim[1]]; $outDim = [$maxDim[0], $maxDim[1]]; // figure out how to crop. if ($dim[0] / $dim[1] >= $maxDim[0] / $maxDim[1]) { $inDim[0] = $maxDim[0] / $maxDim[1] * $dim[1]; $start[0] = ($dim[0] - $inDim[0]) / 2; } else { $inDim[1] = $maxDim[1] / $maxDim[0] * $dim[0]; $start[1] = ($dim[1] - $inDim[1]) / 2; } // don't scale if input image is smaller. if ($inDim[0] < $outDim[0] || $inDim[1] < $outDim[1]) { $outDim = $inDim; } $image = imagecreatetruecolor($outDim[0], $outDim[1]); imagecopyresampled($image, $inputImage, 0, 0, $start[0], $start[1], $outDim[0], $outDim[1], $inDim[0], $inDim[1]); } imagejpeg($image, $path); }
function uploadContent() { $upload_dir = $siteRoot . 'uploads/'; $allowed_ext = array('jpg', 'jpeg', 'JPG', 'JPEG', 'png', 'PNG', 'gif', 'GIF'); $all_thumbs = json_decode(@file_get_contents($siteRoot . 'pages/all_pics.json'), true); if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') { exit_status('Error! Wrong HTTP method!'); } if (array_key_exists('pic', $_FILES) && $_FILES['pic']['error'] == 0) { $pic = $_FILES['pic']; $picNameFix = array("+", "(", ")", "|", "'", "?", " "); $picName = str_replace($picNameFix, '', $pic['name']); if (!in_array(get_extension($picName), $allowed_ext)) { exit_status('Only ' . implode(',', $allowed_ext) . ' files are allowed!'); } // Ensure that file name is unique if ($all_thumbs[$picName]) { for ($i = 1; $i <= 20; $i++) { if ($all_thumbs[$i . '_' . $picName]) { } else { $picName = $i . '_' . $picName; break; } } } // Move the uploaded file from the temporary directory to the uploads folder, // create a thumbnail, and log all pics in json file: if (move_uploaded_file($pic['tmp_name'], $upload_dir . $picName)) { setContent($picName, $upload_dir . $picName, 'all_pics'); $type = false; function open_image($file) { //detect type and process accordinally global $type; $size = getimagesize($file); switch ($size["mime"]) { case "image/jpeg": $im = imagecreatefromjpeg($file); //jpeg file break; case "image/gif": $im = imagecreatefromgif($file); //gif file break; case "image/png": $im = imagecreatefrompng($file); //png file break; default: $im = false; break; } return $im; } $thumbnailImage = open_image($upload_dir . $picName); $w = imagesx($thumbnailImage); $h = imagesy($thumbnailImage); //calculate new image dimensions (preserve aspect) if (isset($_GET['w']) && !isset($_GET['h'])) { $new_w = $_GET['w']; $new_h = $new_w * ($h / $w); } elseif (isset($_GET['h']) && !isset($_GET['w'])) { $new_h = $_GET['h']; $new_w = $new_h * ($w / $h); } else { $new_w = isset($_GET['w']) ? $_GET['w'] : 60; $new_h = isset($_GET['h']) ? $_GET['h'] : 60; if ($w / $h > $new_w / $new_h) { $new_h = $new_w * ($h / $w); } else { $new_w = $new_h * ($w / $h); } } $im2 = ImageCreateTrueColor($new_w, $new_h); $imageFormat = strtolower(substr(strrchr($picName, "."), 1)); if ($imageFormat == "gif" || $imageFormat == "png") { imagecolortransparent($im2, imagecolorallocatealpha($im2, 0, 0, 0, 127)); imagealphablending($im2, false); imagesavealpha($im2, true); } imagecopyResampled($im2, $thumbnailImage, 0, 0, 0, 0, $new_w, $new_h, $w, $h); //effects if (isset($_GET['blur'])) { $lv = $_GET['blur']; for ($i = 0; $i < $lv; $i++) { $matrix = array(array(1, 1, 1), array(1, 1, 1), array(1, 1, 1)); $divisor = 9; $offset = 0; imageconvolution($im2, $matrix, $divisor, $offset); } } if (isset($_GET['sharpen'])) { $lv = $_GET['sharpen']; for ($i = 0; $i < $lv; $i++) { $matrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); $divisor = 8; $offset = 0; imageconvolution($im2, $matrix, $divisor, $offset); } } imagejpeg($im2, $upload_dir . 'thumbs/' . $picName); imagegif($im2, $upload_dir . 'thumbs/' . $picName); imagepng($im2, $upload_dir . 'thumbs/' . $picName); //move_uploaded_file($thumbnailImage, $upload_dir.'thumbs/'.$picName); //copy($upload_dir.$picName, $upload_dir.'thumbs/'.$picName); exit_status('File was uploaded successfuly!'); } } exit_status('Something went wrong with your upload!'); }
/** * Stretch image * * @param mixed $input * @param string $dest_file * @param integer $new_width * @param integer $new_height * @param mixed $output_type * @param integer $quality * @return null */ function stretch_image($input, $dest_file, $new_width, $new_height, $output_type = null, $quality = 80) { if (!extension_loaded('gd')) { return false; } // if if (is_array($input) && array_key_exists('type', $input) && array_key_exists('resource', $input)) { $open_image = $input; $close_resource = false; } else { $open_image = open_image($input); $close_resource = true; if (!is_array($open_image)) { throw new Error(lang('Could not parse image: ' . $input)); return false; } // if } // if $image_type = $open_image['type']; $image = $open_image['resource']; if ($output_type === null) { $output_type = $image_type; } // if $width = imagesx($image); $height = imagesy($image); $resulting_image = imagecreatetruecolor($new_width, $new_height); if (can_use_image_alpha($output_type)) { imagealphablending($resulting_image, false); imagesavealpha($resulting_image, true); $alpha = imagecolorallocatealpha($resulting_image, 255, 255, 255, 127); imagefilledrectangle($resulting_image, 0, 0, $new_width, $new_height, $alpha); } else { $white_color = imagecolorallocate($resulting_image, 255, 255, 255); imagefill($resulting_image, 0, 0, $white_color); } // if imagecopyresampled($resulting_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); if ($close_resource) { imagedestroy($image); } // if $result = save_image($image, $dest_file, $output_type, $quality); return $result; }
private function build_image($resize, $thumb, $table, $name, $file) { //move image to temp folder move_uploaded_file($this->image['tmp_name'], $this->tempfile); $this->imagehash = hash_file('md5', $this->tempfile); $this->check_db($table, $name, $file); if ($this->existing && file_exists($this->newfile)) { echo "File already exists. "; // echo "File already exists. Links redirected to $this->newfname. "; } else { if (!file_exists($this->newfile)) { if ($resize) { if ($this->image['type'] != 'image/gif') { if ($temp = open_image($this->tempfile)) { $src = $temp[0]; $canvas = inline_crop($src, $this->dimensions, $this->cropmode, $this->prefwidth, $this->prefheight); $this->save_image($this->image['type'], $canvas, $this->newfile); imagedestroy($canvas); imagedestroy($src); } else { unlink($this->tempfile); echo 'Error: image too large or invalid. '; return false; } } else { copy($this->tempfile, $this->newfile); echo 'Image uploaded! '; } } else { if ($thumb) { if ($temp = open_image($this->tempfile)) { $src = $temp[0]; $canvas = make_thumbnail($src, $this->dimensions, $this->prefwidth, $this->prefheight, $this->thumbcolor); $this->save_image($this->thumbmime, $canvas, $this->thumbfile); } else { unlink($this->tempfile); echo 'Error: image too large or invalid. '; return false; } } copy($this->tempfile, $this->newfile); } } } unlink($this->tempfile); $this->tempfile = null; return true; }
/** * Create thumbnail for an image if the module is gallery * * @param $pathToImages The path where the image that has to be converted is stored. * @param $pathToThumbs The path where the thumbnail that is created must be saved. * * @return true if the thumbnail creation is successful and false otherwise */ function createThumbs($pathToImages, $pathToThumbs, $thumbWidth) { // load image and get image size $img = open_image("{$pathToImages}"); if ($img != false) { $width = imagesx($img); $height = imagesy($img); // calculate thumbnail size $new_width = $thumbWidth; // $new_height = floor( $height * ( $thumbWidth / $width ) ); $new_height = $thumbWidth; // create a new temporary image $tmp_img = imagecreatetruecolor($new_width, $new_height); // copy and resize old image into new image imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // save thumbnail into a file $size = getimagesize("{$pathToImages}"); switch ($size["mime"]) { case "image/jpeg": $im = imagejpeg($tmp_img, "{$pathToThumbs}"); break; case "image/gif": $im = imagegif($tmp_img, "{$pathToThumbs}"); break; case "image/png": $im = imagepng($tmp_img, "{$pathToThumbs}"); break; } return true; } return false; }
imageinterlace($image, 1); //--將圖片轉換為漸進式模 imagejpeg($image); } } break; case "watermark": // 浮水印 $img_info = getimagesize($_GET["markurl"]); $w_width = $img_info['0']; $w_height = $img_info['1']; $w_mime = $img_info['mime']; $pi_w = $_GET["pw"] ? $_GET["pw"] : 0.4; $pi_h = $_GET["ph"] ? $_GET["ph"] : 0.2; //--打開水印圖檔 $watermark = open_image($_GET["markurl"]); $watermark_pos_x = abs($w / 2 - $w_width / 2); $watermark_pos_y = abs($h / 2 - $w_height / 2); //--大小換算 $target_w = abs($w_width * ($w / $w_width) * $pi_w); $target_h = abs($w_height * ($h / $w_height) * $pi_h); // 浮水印的圖若是透明背景、透明底圖, 需要用下述兩行 $case_in = $_GET["in"] ? $_GET["in"] : 5; $temp_png_w_array = png_water_inpos($w, $h, $target_w, $target_h, $case_in); $watermark_pos_x = $temp_png_w_array[0]; $watermark_pos_y = $temp_png_w_array[1]; $image_p = imagecreatetruecolor($target_w, $target_h); //--建立空圖 imagealphablending($image_p, false); imagecopyresampled($image_p, $watermark, 0, 0, 0, 0, $target_w, $target_h, $w_width, $w_height); $watermark = $image_p;
/** * Force resize * * @param string $input_file * @param string $dest_file * @param integer $width * @param integer $height * @param mixed $output_type * @return null */ function resize_image($input_file, $dest_file, $new_width, $new_height, $output_type = null, $quality = 80) { if (!extension_loaded('gd')) { return false; } // if $open_image = open_image($input_file); if (is_array($open_image)) { $image_type = $open_image['type']; $image = $open_image['resource']; $width = imagesx($image); $height = imagesy($image); $tmp_img = imagecreatetruecolor($new_width, $new_height); $white_color = imagecolorallocate($tmp_img, 255, 255, 255); imagefill($tmp_img, 0, 0, $white_color); imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($image); $image = $tmp_img; if ($output_type === null) { $output_type = $image_type; } // if switch ($output_type) { case IMAGETYPE_JPEG: return imagejpeg($image, $dest_file, $quality); case IMAGETYPE_GIF: return imagegif($image, $dest_file); case IMAGETYPE_PNG: return imagepng($image, $dest_file); } // switch } // ifs return false; }
/** * creates a thumbnail and put it in the THUMBS dir, or print it. * * @param string $src reference to the original file * @param int $maxY max height of thumb * @param int $maxX max width of thumb * @return true if success, false if error */ static function createThumbnail($src, $writeToFile = true, $maxY = false, $maxX = false) { if (!$maxY && !$maxX) { return false; } $file = open_image($src); if ($file) { $oldX = imagesx($file); $oldY = imagesy($file); $x = 0; $y = 0; $assumedY = $maxX * ($oldY / $oldX); $assumedX = $maxY * ($oldX / $oldY); if ($assumedX > $maxX) { $x = $maxX; $y = $assumedY; } if ($assumedY > $maxY) { $x = $assumedX; $y = $maxY; } $img = imagecreatetruecolor($x, $y); imagecopyresampled($img, $file, 0, 0, 0, 0, $x, $y, $oldX, $oldY); $filename = strtolower(strtr(substr($src, STRLEN(IMAGES) + 1), '/', '_')); $destination = THUMBS . '/' . $filename; if ($writeToFile) { imagejpeg($img, $destination, 40); } else { header('content-type: image/jpeg'); imagejpeg($img); } } return true; }