function exists()
 {
     $oldmemlimit = @ini_get('memory_limit');
     if (empty($oldmemlimit)) {
         // PHP not compiled with memory limits, this means that it's
         // probably limited to 8M or in case of Windows not at all.
         // We can ignore it for now - there is not much to test anyway
         // TODO: add manual test that fills memory??
         return false;
     }
     $oldmemlimit = get_real_size($oldmemlimit);
     //now lets change the memory limit to something unique below 128M==134217728
     if (empty($CFG->extramemorylimit)) {
         raise_memory_limit('128M');
     } else {
         raise_memory_limit($CFG->extramemorylimit);
     }
     $testmemlimit = get_real_size(@ini_get('memory_limit'));
     //verify the change had any effect at all
     if ($oldmemlimit == $testmemlimit) {
         //memory limit can not be changed - is it big enough then?
         if ($oldmemlimit < get_real_size('128M')) {
             return true;
         } else {
             return false;
         }
     }
     reduce_memory_limit($oldmemlimit);
     return false;
 }
 private function get_image_resized(\stored_file $file = null, $height = 250, $width = 250, $offsetx = 0, $offsety = 0, $crop = true)
 {
     global $CFG;
     if (is_null($file) && !($file = $this->get_stored_file_by_type('item'))) {
         return false;
     }
     require_once $CFG->libdir . '/gdlib.php';
     $oldmemlimit = @ini_get('memory_limit');
     raise_memory_limit(MEMORY_EXTRA);
     $tempfile = $file->copy_content_to_temp();
     if (!imagehelper::memory_check($tempfile)) {
         return false;
     }
     $image = imagecreatefromstring(file_get_contents($tempfile));
     // Func exif_read_data is only supported for jpeg/tiff images.
     $mimetype = $file->get_mimetype();
     $isjpegortiff = $mimetype == 'image/jpeg' || $mimetype == 'image/tiff';
     if ($isjpegortiff && ($exif = exif_read_data($tempfile))) {
         $ort = 1;
         if (isset($exif['IFD0']['Orientation'])) {
             $ort = $exif['IFD0']['Orientation'];
         } else {
             if (isset($exif['Orientation'])) {
                 $ort = $exif['Orientation'];
             }
         }
         $mirror = false;
         $degree = 0;
         switch ($ort) {
             case 2:
                 // Horizontal flip.
                 $mirror = true;
                 break;
             case 3:
                 // 180 Rotate left.
                 $degree = 180;
                 break;
             case 4:
                 // Vertical flip.
                 $degree = 180;
                 $mirror = true;
                 break;
             case 5:
                 // Vertical flip + 90 rotate right.
                 $degree = 270;
                 $mirror = true;
                 break;
             case 6:
                 // 90 rotate right.
                 $degree = 270;
                 break;
             case 7:
                 // Horizontal flip + 90 rotate right.
                 $degree = 90;
                 $mirror = true;
                 break;
             case 8:
                 // 90 rotate left.
                 $degree = 90;
                 break;
             default:
                 // Do nothing.
                 break;
         }
         if ($degree) {
             $image = imagerotate($image, $degree, 0);
         }
         if ($mirror) {
             $image = imagehelper::mirror($image);
         }
     }
     $info = array('width' => imagesx($image), 'height' => imagesy($image));
     $cx = $info['width'] / 2;
     $cy = $info['height'] / 2;
     $ratiow = $width / $info['width'];
     $ratioh = $height / $info['height'];
     if ($info['width'] <= $width && $info['height'] <= $height) {
         // Images containing EXIF orientation data don't display correctly in browsers.
         // So even though we're not making a smaller version of the original here, we still
         // want to have it displayed right-side up.
         $width = $info['width'];
         $height = $info['height'];
         $ratiow = $width / $info['width'];
         $ratioh = $height / $info['height'];
     }
     if (!$crop) {
         if ($ratiow < $ratioh) {
             $height = floor($info['height'] * $ratiow);
             $width = floor($info['width'] * $ratiow);
         } else {
             $height = floor($info['height'] * $ratioh);
             $width = floor($info['width'] * $ratioh);
         }
         $srcw = $info['width'];
         $srch = $info['height'];
         $srcx = 0;
         $srcy = 0;
     } else {
         if ($ratiow < $ratioh) {
             $srcw = floor($width / $ratioh);
             $srch = $info['height'];
             $srcx = floor($cx - $srcw / 2) + $offsetx;
             $srcy = $offsety;
         } else {
             $srcw = $info['width'];
             $srch = floor($height / $ratiow);
             $srcx = $offsetx;
             $srcy = floor($cy - $srch / 2) + $offsety;
         }
     }
     $resized = imagecreatetruecolor($width, $height);
     imagecopybicubic($resized, $image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch);
     unset($image);
     reduce_memory_limit($oldmemlimit);
     return $resized;
 }