/** * Given a user id this function scales and crops the user images to remove * the one pixel black border. * * @global object * @param int $id * @param string $dir * @return boolean */ function upgrade_profile_image($id, $dir = 'users') { global $CFG, $OUTPUT; $im = ImageCreateFromJPEG($CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg'); if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { $im1 = ImageCreateTrueColor(100, 100); $im2 = ImageCreateTrueColor(35, 35); } else { $im1 = ImageCreate(100, 100); $im2 = ImageCreate(35, 35); } if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96); } else { imagecopy($im1, $im, 0, 0, 0, 0, 100, 100); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 2, 2)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 0, 0, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 2, 97)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 0, 99, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 97, 2)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 99, 0, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 97, 97)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 99, 99, $color); for ($x = 1; $x < 99; $x++) { $c1 = ImageColorsForIndex($im1, ImageColorAt($im, $x, 1)); $color = ImageColorClosest($im, $c1['red'], $c1['green'], $c1['blue']); ImageSetPixel($im1, $x, 0, $color); $c2 = ImageColorsForIndex($im1, ImageColorAt($im1, $x, 98)); $color = ImageColorClosest($im, $c2['red'], $c2['green'], $c2['blue']); ImageSetPixel($im1, $x, 99, $color); } for ($y = 1; $y < 99; $y++) { $c3 = ImageColorsForIndex($im1, ImageColorAt($im, 1, $y)); $color = ImageColorClosest($im, $c3['red'], $c3['green'], $c3['blue']); ImageSetPixel($im1, 0, $y, $color); $c4 = ImageColorsForIndex($im1, ImageColorAt($im1, 98, $y)); $color = ImageColorClosest($im, $c4['red'], $c4['green'], $c4['blue']); ImageSetPixel($im1, 99, $y, $color); } } ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96); if (function_exists('ImageJpeg')) { if (ImageJpeg($im1, $CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg', 90) and ImageJpeg($im2, $CFG->dataroot . '/' . $dir . '/' . $id . '/f2.jpg', 95)) { @chmod($CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg', 0666); @chmod($CFG->dataroot . '/' . $dir . '/' . $id . '/f2.jpg', 0666); return 1; } } else { echo $OUTPUT->notification('PHP has not been configured to support JPEG images. Please correct this.'); } return 0; }
function lightboxgallery_resize_image($image, $infoobj, $width, $height, $offsetx = 0, $offsety = 0) { global $CFG; $resized = lightboxgallery_image_create($width, $height); $oldwidth = $infoobj->imagesize[0]; $oldheight = $infoobj->imagesize[1]; $cx = $oldwidth / 2; $cy = $oldheight / 2; $ratiow = $width / $oldwidth; $ratioh = $height / $oldheight; if ($ratiow < $ratioh) { $srcw = floor($width / $ratioh); $srch = $oldheight; $srcx = floor($cx - $srcw / 2) + $offsetx; $srcy = $offsety; } else { $srcw = $oldwidth; $srch = floor($height / $ratiow); $srcx = $offsetx; $srcy = floor($cy - $srch / 2) + $offsety; } ImageCopyBicubic($resized, $image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch); return $resized; }
/** * (Re)generate thumbnail image according to the dimensions specified in the field settings. * If thumbnail width and height are BOTH not specified then no thumbnail is generated, and * additionally an attempted delete of the existing thumbnail takes place. */ function update_thumbnail($content) { global $CFG; require_once $CFG->libdir . '/gdlib.php'; $datalocation = $CFG->dataroot . '/' . $this->data->course . '/' . $CFG->moddata . '/data/' . $this->data->id . '/' . $this->field->id . '/' . $content->recordid; $originalfile = $datalocation . '/' . $content->content; if (!file_exists($originalfile)) { return; } if (!file_exists($datalocation . '/thumb')) { mkdir($datalocation . '/thumb', 0777); } $thumbnaillocation = $datalocation . '/thumb/' . $content->content; $imageinfo = GetImageSize($originalfile); $image->width = $imageinfo[0]; $image->height = $imageinfo[1]; $image->type = $imageinfo[2]; if (!$image->width || !$image->height) { // Should not happen return; } switch ($image->type) { case 1: if (function_exists('ImageCreateFromGIF')) { $im = ImageCreateFromGIF($originalfile); } else { return; } break; case 2: if (function_exists('ImageCreateFromJPEG')) { $im = ImageCreateFromJPEG($originalfile); } else { return; } break; case 3: if (function_exists('ImageCreateFromPNG')) { $im = ImageCreateFromPNG($originalfile); } else { return; } break; } // fix for MDL-7270 $thumbwidth = isset($this->field->param4) ? $this->field->param4 : ''; $thumbheight = isset($this->field->param5) ? $this->field->param5 : ''; if ($thumbwidth || $thumbheight) { // Only if either width OR height specified do we want a thumbnail $wcrop = $image->width; $hcrop = $image->height; if ($thumbwidth && !$thumbheight) { $thumbheight = $image->height * $thumbwidth / $image->width; } else { if ($thumbheight && !$thumbwidth) { $thumbwidth = $image->width * $thumbheight / $image->height; } else { // BOTH are set - may need to crop if aspect ratio differs $hratio = $image->height / $thumbheight; $wratio = $image->width / $thumbwidth; if ($wratio > $hratio) { // Crop the width $wcrop = intval($thumbwidth * $hratio); } elseif ($hratio > $wratio) { // Crop the height $hcrop = intval($thumbheight * $wratio); } } } // At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { $im1 = ImageCreateTrueColor($thumbwidth, $thumbheight); } else { $im1 = ImageCreate($thumbwidth, $thumbheight); } // Prevent alpha blending for PNG images if ($image->type == 3) { imagealphablending($im1, false); } $cx = $image->width / 2; $cy = $image->height / 2; // These "half" measurements use the "crop" values rather than the actual dimensions $halfwidth = floor($wcrop * 0.5); $halfheight = floor($hcrop * 0.5); ImageCopyBicubic($im1, $im, 0, 0, $cx - $halfwidth, $cy - $halfheight, $thumbwidth, $thumbheight, $halfwidth * 2, $halfheight * 2); // Save alpha transparency for PNG images if ($image->type == 3) { imagesavealpha($im1, true); } if (function_exists('ImageJpeg') && $image->type != 3) { @touch($thumbnaillocation); // Helps in Safe mode if (ImageJpeg($im1, $thumbnaillocation, 90)) { @chmod($thumbnaillocation, 0666); } } elseif (function_exists('ImagePng') && $image->type == 3) { @touch($thumbnaillocation); // Helps in Safe mode if (ImagePng($im1, $thumbnaillocation, 9)) { @chmod($thumbnaillocation, 0666); } } } else { // Try and remove the thumbnail - we don't want thumbnailing active @unlink($thumbnaillocation); } }
/** * Stores optimised icon images in icon file area * * @param $context * @param component * @param $itemid * @param $originalfile * @return success */ function process_new_icon($context, $component, $filearea, $itemid, $originalfile) { global $CFG; if (empty($CFG->gdversion)) { return false; } if (!is_file($originalfile)) { return false; } $imageinfo = GetImageSize($originalfile); if (empty($imageinfo)) { return false; } $image = new stdClass(); $image->width = $imageinfo[0]; $image->height = $imageinfo[1]; $image->type = $imageinfo[2]; switch ($image->type) { case IMAGETYPE_GIF: if (function_exists('ImageCreateFromGIF')) { $im = ImageCreateFromGIF($originalfile); } else { debugging('GIF not supported on this server'); return false; } break; case IMAGETYPE_JPEG: if (function_exists('ImageCreateFromJPEG')) { $im = ImageCreateFromJPEG($originalfile); } else { debugging('JPEG not supported on this server'); return false; } break; case IMAGETYPE_PNG: if (function_exists('ImageCreateFromPNG')) { $im = ImageCreateFromPNG($originalfile); } else { debugging('PNG not supported on this server'); return false; } break; default: return false; } if (function_exists('ImagePng')) { $imagefnc = 'ImagePng'; $imageext = '.png'; $filters = PNG_NO_FILTER; $quality = 1; } else { if (function_exists('ImageJpeg')) { $imagefnc = 'ImageJpeg'; $imageext = '.jpg'; $filters = null; // not used $quality = 90; } else { debugging('Jpeg and png not supported on this server, please fix server configuration'); return false; } } if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { $im1 = ImageCreateTrueColor(100, 100); $im2 = ImageCreateTrueColor(35, 35); if ($image->type == IMAGETYPE_PNG and $imagefnc === 'ImagePng') { imagealphablending($im1, false); $color = imagecolorallocatealpha($im1, 0, 0, 0, 127); imagefill($im1, 0, 0, $color); imagesavealpha($im1, true); imagealphablending($im2, false); $color = imagecolorallocatealpha($im2, 0, 0, 0, 127); imagefill($im2, 0, 0, $color); imagesavealpha($im2, true); } } else { $im1 = ImageCreate(100, 100); $im2 = ImageCreate(35, 35); } $cx = $image->width / 2; $cy = $image->height / 2; if ($image->width < $image->height) { $half = floor($image->width / 2.0); } else { $half = floor($image->height / 2.0); } ImageCopyBicubic($im1, $im, 0, 0, $cx - $half, $cy - $half, 100, 100, $half * 2, $half * 2); ImageCopyBicubic($im2, $im, 0, 0, $cx - $half, $cy - $half, 35, 35, $half * 2, $half * 2); $fs = get_file_storage(); $icon = array('contextid' => $context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/'); ob_start(); if (!$imagefnc($im1, NULL, $quality, $filters)) { // keep old icons ob_end_clean(); return false; } $data = ob_get_clean(); ImageDestroy($im1); $icon['filename'] = 'f1' . $imageext; $fs->delete_area_files($context->id, $component, $filearea, $itemid); $fs->create_file_from_string($icon, $data); ob_start(); if (!$imagefnc($im2, NULL, $quality, $filters)) { ob_end_clean(); $fs->delete_area_files($context->id, $component, $filearea, $itemid); return false; } $data = ob_get_clean(); ImageDestroy($im2); $icon['filename'] = 'f2' . $imageext; $fs->create_file_from_string($icon, $data); return true; }