function install_print_help_page($help) { global $CFG; @header('Content-Type: text/html; charset=UTF-8'); @header('Cache-Control: no-store, no-cache, must-revalidate'); @header('Cache-Control: post-check=0, pre-check=0', false); @header('Pragma: no-cache'); @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; echo '<html dir="' . (right_to_left() ? 'rtl' : 'ltr') . '"> <head> <link rel="shortcut icon" href="theme/standard/favicon.ico" /> <link rel="stylesheet" type="text/css" href="' . $CFG->wwwroot . '/install.php?css=1" /> <title>' . get_string('installation', 'install') . '</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="expires" content="0" />'; echo '</head><body>'; switch ($help) { case 'phpversionhelp': print_string($help, 'install', phpversion()); break; case 'memorylimithelp': print_string($help, 'install', get_memory_limit()); break; default: print_string($help, 'install'); } close_window_button(); echo '</body></html>'; die; }
/** * This function could be made much smaller by using the ImageReplaceEvent * ie: Pretend that we are replacing the image with a rotated copy. * * @param int $image_id * @param int $deg * @throws ImageRotateException */ private function rotate_image($image_id, $deg) { global $config, $user, $page, $database; if ($deg <= -360 || $deg >= 360) { throw new ImageRotateException("Invalid options for rotation angle. ({$deg})"); } $image_obj = Image::by_id($image_id); $hash = $image_obj->hash; if (is_null($hash)) { throw new ImageRotateException("Image does not have a hash associated with it."); } $image_filename = warehouse_path("images", $hash); if (file_exists($image_filename) == false) { throw new ImageRotateException("{$image_filename} does not exist."); } $info = getimagesize($image_filename); /* Get the image file type */ $pathinfo = pathinfo($image_obj->filename); $filetype = strtolower($pathinfo['extension']); /* Check Memory usage limits Old check: $memory_use = (filesize($image_filename)*2) + ($width*$height*4) + (4*1024*1024); New check: memory_use = width * height * (bits per channel) * channels * 2.5 It didn't make sense to compute the memory usage based on the NEW size for the image. ($width*$height*4) We need to consider the size that we are GOING TO instead. The factor of 2.5 is simply a rough guideline. http://stackoverflow.com/questions/527532/reasonable-php-memory-limit-for-image-resize */ $memory_use = $info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5 / 1024; $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { throw new ImageRotateException("The image is too large to rotate given the memory limits. ({$memory_use} > {$memory_limit})"); } /* Attempt to load the image */ switch ($info[2]) { case IMAGETYPE_GIF: $image = imagecreatefromgif($image_filename); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($image_filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($image_filename); break; default: throw new ImageRotateException("Unsupported image type or "); } /* Rotate and resample the image */ /* $image_rotated = imagecreatetruecolor( $new_width, $new_height ); if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) { $transparency = imagecolortransparent($image); if ($transparency >= 0) { $transparent_color = imagecolorsforindex($image, $trnprt_indx); $transparency = imagecolorallocate($image_rotated, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($image_rotated, 0, 0, $transparency); imagecolortransparent($image_rotated, $transparency); } elseif ($info[2] == IMAGETYPE_PNG) { imagealphablending($image_rotated, false); $color = imagecolorallocatealpha($image_rotated, 0, 0, 0, 127); imagefill($image_rotated, 0, 0, $color); imagesavealpha($image_rotated, true); } } */ $image_rotated = imagerotate($image, $deg, 0); /* Temp storage while we rotate */ $tmp_filename = tempnam(ini_get('upload_tmp_dir'), 'shimmie_rotate'); if (empty($tmp_filename)) { throw new ImageRotateException("Unable to save temporary image file."); } /* Output to the same format as the original image */ switch ($info[2]) { case IMAGETYPE_GIF: imagegif($image_rotated, $tmp_filename); break; case IMAGETYPE_JPEG: imagejpeg($image_rotated, $tmp_filename); break; case IMAGETYPE_PNG: imagepng($image_rotated, $tmp_filename); break; default: throw new ImageRotateException("Unsupported image type."); } /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); $target = warehouse_path("images", $new_hash); if (!@copy($tmp_filename, $target)) { throw new ImageRotateException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ({$target})"); } $new_filename = 'rotated-' . $image_obj->filename; list($new_width, $new_height) = getimagesize($target); /* Remove temporary file */ @unlink($tmp_filename); /* Delete original image and thumbnail */ log_debug("image", "Removing image with hash " . $hash); $image_obj->remove_image_only(); /* Generate new thumbnail */ send_event(new ThumbnailGenerationEvent($new_hash, $filetype)); /* Update the database */ $database->Execute("UPDATE images SET \n\t\t\t\t\tfilename = :filename, filesize = :filesize,\thash = :hash, width = :width, height = :height\n\t\t\t\tWHERE \n\t\t\t\t\tid = :id\n\t\t\t\t", array("filename" => $new_filename, "filesize" => $new_size, "hash" => $new_hash, "width" => $new_width, "height" => $new_height, "id" => $image_id)); log_info("rotate", "Rotated Image #{$image_id} - New hash: {$new_hash}"); }
function check_memory_limit() { /// if limit is already 40M or more then we don't care if we can change it or not if ((int) str_replace('M', '', get_memory_limit()) >= 40) { return true; } /// Otherwise, see if we can change it ourselves @ini_set('memory_limit', '40M'); return (int) str_replace('M', '', get_memory_limit()) >= 40; }
private function get_thumb($tmpname) { global $config; $info = getimagesize($tmpname); $width = $info[0]; $height = $info[1]; $memory_use = filesize($tmpname) * 2 + $width * $height * 4 + 4 * 1024 * 1024; $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { $w = $config->get_int('thumb_width'); $h = $config->get_int('thumb_height'); $thumb = imagecreatetruecolor($w, min($h, 64)); $white = imagecolorallocate($thumb, 255, 255, 255); $black = imagecolorallocate($thumb, 0, 0, 0); imagefill($thumb, 0, 0, $white); imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black); return $thumb; } else { $image = imagecreatefromstring($this->read_file($tmpname)); $tsize = get_thumbnail_size($width, $height); $thumb = imagecreatetruecolor($tsize[0], $tsize[1]); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $tsize[0], $tsize[1], $width, $height); return $thumb; } }
function theme_check_memory_limit($test_alloc_memory = false) { $need_memory_size = 64 * 1024 * 1024; $memory = get_memory_limit(); // can't retrieve memory limit option if (-1 == $memory) { return; } // try to increase limit if ($memory < $need_memory_size) { if (!function_exists('ini_set')) { theme_out_of_memory_handler(false); } $ret = ini_set('memory_limit', '64M'); if (!$ret) { theme_out_of_memory_handler(false); } } // check real limits if ($test_alloc_memory) { theme_test_memory_size(); } }
private function resize_image($image_id, $width, $height) { global $config; global $user; global $page; global $database; if ($height <= 0 && $width <= 0) { throw new ImageResizeException("Invalid options for height and width. ({$width} x {$height})"); } $image_obj = Image::by_id($image_id); $hash = $image_obj->hash; if (is_null($hash)) { throw new ImageResizeException("Image does not have a hash associated with it."); } $image_filename = warehouse_path("images", $hash); $info = getimagesize($image_filename); /* Get the image file type */ $pathinfo = pathinfo($image_obj->filename); $filetype = strtolower($pathinfo['extension']); if ($image_obj->width != $info[0] || $image_obj->height != $info[1]) { throw new ImageResizeException("The image size does not match what is in the database! - Aborting Resize."); } /* Check memory usage limits */ $memory_use = filesize($image_filename) * 2 + $width * $height * 4 + 4 * 1024 * 1024; $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { throw new ImageResizeException("The image is too large to resize given the memory limits. ({$memory_use} > {$memory_limit})"); } /* Calculate the new size of the image */ if ($height > 0 && $width > 0) { $new_height = $height; $new_width = $width; } else { // Scale the new image if ($width == 0) { $factor = $height / $image_obj->height; } elseif ($height == 0) { $factor = $width / $image_obj->width; } else { $factor = min($width / $image_obj->width, $height / $image_obj->height); } $new_width = round($image_obj->width * $factor); $new_height = round($image_obj->height * $factor); } /* Attempt to load the image */ switch ($info[2]) { case IMAGETYPE_GIF: $image = imagecreatefromgif($image_filename); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($image_filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($image_filename); break; default: throw new ImageResizeException("Unsupported image type."); } /* Resize and resample the image */ $image_resized = imagecreatetruecolor($new_width, $new_height); if ($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG) { $transparency = imagecolortransparent($image); if ($transparency >= 0) { $transparent_color = imagecolorsforindex($image, $trnprt_indx); $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($image_resized, 0, 0, $transparency); imagecolortransparent($image_resized, $transparency); } elseif ($info[2] == IMAGETYPE_PNG) { imagealphablending($image_resized, false); $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); imagefill($image_resized, 0, 0, $color); imagesavealpha($image_resized, true); } } imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $image_obj->width, $image_obj->height); /* Temp storage while we resize */ $tmp_filename = tempnam("/tmp", 'shimmie_resize'); if (empty($tmp_filename)) { throw new ImageResizeException("Unable to save temporary image file."); } /* Output to the same format as the original image */ switch ($info[2]) { case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $tmp_filename); break; case IMAGETYPE_PNG: imagepng($image_resized, $tmp_filename); break; default: throw new ImageResizeException("Unsupported image type."); } /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); $target = warehouse_path("images", $new_hash); if (!file_exists(dirname($target))) { mkdir(dirname($target), 0755, true); } if (!@copy($tmp_filename, $target)) { throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ({$target})"); } $new_filename = 'resized-' . $image_obj->filename; /* Remove temporary file */ @unlink($tmp_filename); /* Delete original image and thumbnail */ log_debug("image", "Removing image with hash " . $hash); $image_obj->remove_image_only(); /* Generate new thumbnail */ send_event(new ThumbnailGenerationEvent($new_hash, $filetype)); /* Update the database */ $database->Execute("UPDATE images SET \n\t\t\t\t\tfilename = :filename, filesize = :filesize,\thash = :hash, width = :width, height = :height\n\t\t\t\tWHERE \n\t\t\t\t\tid = :id\n\t\t\t\t", array("filename" => $new_filename, "filesize" => $new_size, "hash" => $new_hash, "width" => $new_width, "height" => $new_height, "id" => $image_id)); log_info("resize", "Resized Image #{$image_id} - New hash: {$new_hash}"); }
/** * This function could be made much smaller by using the ImageReplaceEvent * ie: Pretend that we are replacing the image with a resized copy. * * @param Image $image_obj * @param int $width * @param int $height * @throws ImageResizeException */ private function resize_image(Image $image_obj, $width, $height) { global $config, $user, $page, $database; if ($height <= 0 && $width <= 0) { throw new ImageResizeException("Invalid options for height and width. ({$width} x {$height})"); } $hash = $image_obj->hash; $image_filename = warehouse_path("images", $hash); $info = getimagesize($image_filename); /* Get the image file type */ $pathinfo = pathinfo($image_obj->filename); $filetype = strtolower($pathinfo['extension']); if ($image_obj->width != $info[0] || $image_obj->height != $info[1]) { throw new ImageResizeException("The current image size does not match what is set in the database! - Aborting Resize."); } /* Check Memory usage limits Old check: $memory_use = (filesize($image_filename)*2) + ($width*$height*4) + (4*1024*1024); New check: memory_use = width * height * (bits per channel) * channels * 2.5 It didn't make sense to compute the memory usage based on the NEW size for the image. ($width*$height*4) We need to consider the size that we are GOING TO instead. The factor of 2.5 is simply a rough guideline. http://stackoverflow.com/questions/527532/reasonable-php-memory-limit-for-image-resize */ if (isset($info['bits']) && isset($info['channels'])) { $memory_use = $info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5 / 1024; } else { // // If we don't have bits and channel info from the image then assume default values // of 8 bits per color and 4 channels (R,G,B,A) -- ie: regular 24-bit color // $memory_use = $info[0] * $info[1] * 1 * 4 * 2.5 / 1024; } $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { throw new ImageResizeException("The image is too large to resize given the memory limits. ({$memory_use} > {$memory_limit})"); } /* Calculate the new size of the image */ if ($height > 0 && $width > 0) { $new_height = $height; $new_width = $width; } else { // Scale the new image if ($width == 0) { $factor = $height / $image_obj->height; } elseif ($height == 0) { $factor = $width / $image_obj->width; } else { $factor = min($width / $image_obj->width, $height / $image_obj->height); } $new_width = round($image_obj->width * $factor); $new_height = round($image_obj->height * $factor); } /* Attempt to load the image */ switch ($info[2]) { case IMAGETYPE_GIF: $image = imagecreatefromgif($image_filename); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($image_filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($image_filename); break; default: throw new ImageResizeException("Unsupported image type (Only GIF, JPEG, and PNG are supported)."); } // Handle transparent images $image_resized = imagecreatetruecolor($new_width, $new_height); if ($info[2] == IMAGETYPE_GIF) { $transparency = imagecolortransparent($image); // If we have a specific transparent color if ($transparency >= 0) { // Get the original image's transparent color's RGB values $transparent_color = imagecolorsforindex($image, $transparency); // Allocate the same color in the new image resource $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image_resized, 0, 0, $transparency); // Set the background color for new image to transparent imagecolortransparent($image_resized, $transparency); } } elseif ($info[2] == IMAGETYPE_PNG) { // // More info here: http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php // imagealphablending($image_resized, false); imagesavealpha($image_resized, true); $transparent_color = imagecolorallocatealpha($image_resized, 255, 255, 255, 127); imagefilledrectangle($image_resized, 0, 0, $new_width, $new_height, $transparent_color); } // Actually resize the image. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $image_obj->width, $image_obj->height); /* Temp storage while we resize */ $tmp_filename = tempnam("/tmp", 'shimmie_resize'); if (empty($tmp_filename)) { throw new ImageResizeException("Unable to save temporary image file."); } /* Output to the same format as the original image */ switch ($info[2]) { case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $tmp_filename); break; case IMAGETYPE_PNG: imagepng($image_resized, $tmp_filename); break; default: throw new ImageResizeException("Failed to save the new image - Unsupported image type."); } /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); $target = warehouse_path("images", $new_hash); if (!@copy($tmp_filename, $target)) { throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ({$target})"); } $new_filename = 'resized-' . $image_obj->filename; /* Remove temporary file */ @unlink($tmp_filename); /* Delete original image and thumbnail */ log_debug("image", "Removing image with hash " . $hash); $image_obj->remove_image_only(); /* Generate new thumbnail */ send_event(new ThumbnailGenerationEvent($new_hash, $filetype)); /* Update the database */ $database->Execute("UPDATE images SET \n\t\t\t\t\tfilename = :filename, filesize = :filesize,\thash = :hash, width = :width, height = :height\n\t\t\t\tWHERE \n\t\t\t\t\tid = :id\n\t\t\t\t", array("filename" => $new_filename, "filesize" => $new_size, "hash" => $new_hash, "width" => $new_width, "height" => $new_height, "id" => $image_obj->id)); log_info("resize", "Resized Image #{$image_obj->id} - New hash: {$new_hash}"); }
/** * This function could be made much smaller by using the ImageReplaceEvent * ie: Pretend that we are replacing the image with a resized copy. * * @param Image $image_obj * @param int $width * @param int $height * @throws ImageResizeException */ private function resize_image(Image $image_obj, $width, $height) { global $database; if ($height <= 0 && $width <= 0) { throw new ImageResizeException("Invalid options for height and width. ({$width} x {$height})"); } $hash = $image_obj->hash; $image_filename = warehouse_path("images", $hash); $info = getimagesize($image_filename); /* Get the image file type */ $pathinfo = pathinfo($image_obj->filename); $filetype = strtolower($pathinfo['extension']); if ($image_obj->width != $info[0] || $image_obj->height != $info[1]) { throw new ImageResizeException("The current image size does not match what is set in the database! - Aborting Resize."); } $memory_use = $this->calc_memory_use($info); $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { throw new ImageResizeException("The image is too large to resize given the memory limits. ({$memory_use} > {$memory_limit})"); } list($new_height, $new_width) = $this->calc_new_size($image_obj, $width, $height); /* Attempt to load the image */ switch ($info[2]) { case IMAGETYPE_GIF: $image = imagecreatefromgif($image_filename); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($image_filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($image_filename); break; default: throw new ImageResizeException("Unsupported image type (Only GIF, JPEG, and PNG are supported)."); } // Handle transparent images $image_resized = imagecreatetruecolor($new_width, $new_height); if ($info[2] == IMAGETYPE_GIF) { $transparency = imagecolortransparent($image); // If we have a specific transparent color if ($transparency >= 0) { // Get the original image's transparent color's RGB values $transparent_color = imagecolorsforindex($image, $transparency); // Allocate the same color in the new image resource $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image_resized, 0, 0, $transparency); // Set the background color for new image to transparent imagecolortransparent($image_resized, $transparency); } } elseif ($info[2] == IMAGETYPE_PNG) { // // More info here: http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php // imagealphablending($image_resized, false); imagesavealpha($image_resized, true); $transparent_color = imagecolorallocatealpha($image_resized, 255, 255, 255, 127); imagefilledrectangle($image_resized, 0, 0, $new_width, $new_height, $transparent_color); } // Actually resize the image. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $image_obj->width, $image_obj->height); /* Temp storage while we resize */ $tmp_filename = tempnam("/tmp", 'shimmie_resize'); if (empty($tmp_filename)) { throw new ImageResizeException("Unable to save temporary image file."); } /* Output to the same format as the original image */ switch ($info[2]) { case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $tmp_filename); break; case IMAGETYPE_PNG: imagepng($image_resized, $tmp_filename); break; default: throw new ImageResizeException("Failed to save the new image - Unsupported image type."); } /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); $target = warehouse_path("images", $new_hash); if (!@copy($tmp_filename, $target)) { throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ({$target})"); } $new_filename = 'resized-' . $image_obj->filename; /* Remove temporary file */ @unlink($tmp_filename); /* Delete original image and thumbnail */ log_debug("image", "Removing image with hash " . $hash); $image_obj->remove_image_only(); /* Generate new thumbnail */ send_event(new ThumbnailGenerationEvent($new_hash, $filetype)); /* Update the database */ $database->Execute("\n\t\t\tUPDATE images SET filename = :filename, filesize = :filesize, hash = :hash, width = :width, height = :height\n\t\t\tWHERE id = :id\n\t\t", array("filename" => $new_filename, "filesize" => $new_size, "hash" => $new_hash, "width" => $new_width, "height" => $new_height, "id" => $image_obj->id)); log_info("resize", "Resized Image #{$image_obj->id} - New hash: {$new_hash}"); }