123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?php
- include dirname(__FILE__) . "/watermark.php";
- function tp_create_gd_thumbnails($file, $prefix, $filestorename) {
- global $CONFIG;
- $image_sizes = elgg_get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
-
- register_error(elgg_echo('tidypics:nosettings'));
- forward(REFERER);
- return FALSE;
- }
- $image_sizes = unserialize($image_sizes);
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- if (empty($image_sizes['tiny_image_width'])) {
-
- $image_sizes['tiny_image_width'] = $image_sizes['tiny_image_height'] = 60;
- }
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- FALSE,
- $image_sizes['tiny_image_width'],
- $image_sizes['tiny_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->thumbnail = $prefix."thumb".$filestorename;
-
- global $CONFIG;
- $CONFIG->debug = 'WARNING';
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- FALSE,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->smallthumb = $prefix."smallthumb".$filestorename;
- unset($CONFIG->debug);
-
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- TRUE,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- FALSE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->largethumb = $prefix."largethumb".$filestorename;
- unset($thumb);
- return TRUE;
- }
- function tp_gd_resize($input_name, $output_name, $watermark, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- $imgsizearray = getimagesize($input_name);
- if (!$imgsizearray) {
- return FALSE;
- }
-
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $params = tp_im_calc_resize_params($width, $height, $maxwidth, $maxheight, $square, $x1, $y1, $x2, $y2);
- if (!$params) {
- return FALSE;
- }
- $new_width = $params['new_width'];
- $new_height = $params['new_height'];
- $region_width = $params['region_width'];
- $region_height = $params['region_height'];
- $widthoffset = $params['width_offset'];
- $heightoffset = $params['height_offset'];
- $accepted_formats = array(
- 'image/jpeg' => 'jpeg',
- 'image/pjpeg' => 'jpeg',
- 'image/png' => 'png',
- 'image/x-png' => 'png',
- 'image/gif' => 'gif'
- );
-
- $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
- if (!is_callable($function)) {
- return FALSE;
- }
-
- $oldimage = $function($input_name);
- if (!$oldimage) {
- return FALSE;
- }
-
- $newimage = imagecreatetruecolor($new_width, $new_height);
- if (!$newimage) {
- return FALSE;
- }
-
- imagefilledrectangle(
- $newimage, 0, 0, $new_width, $new_height, imagecolorallocate($newimage, 255, 255, 255)
- );
- $rtn_code = imagecopyresampled( $newimage,
- $oldimage,
- 0,
- 0,
- $widthoffset,
- $heightoffset,
- $new_width,
- $new_height,
- $region_width,
- $region_height);
- if (!$rtn_code) {
- return $rtn_code;
- }
- if ($watermark) {
- tp_gd_watermark($newimage);
- }
- switch ($imgsizearray['mime']) {
- case 'image/jpeg':
- case 'image/pjpeg':
- $rtn_code = imagejpeg($newimage, $output_name, 85);
- break;
- case 'image/png':
- case 'image/x-png':
- $rtn_code = imagepng($newimage, $output_name);
- break;
- case 'image/gif':
- $rtn_code = imagegif($newimage, $output_name);
- break;
- }
- imagedestroy($newimage);
- imagedestroy($oldimage);
- return $rtn_code;
- }
- function tp_create_imagick_thumbnails($file, $prefix, $filestorename) {
- $image_sizes = elgg_get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
- register_error(elgg_echo('tidypics:nosettings'));
- return FALSE;
- }
- $image_sizes = unserialize($image_sizes);
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- if (empty($image_sizes['tiny_image_width'])) {
-
- $image_sizes['tiny_image_width'] = $image_sizes['tiny_image_height'] = 60;
- }
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['tiny_image_width'],
- $image_sizes['tiny_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->thumbnail = $prefix."thumb".$filestorename;
-
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->smallthumb = $prefix."smallthumb".$filestorename;
-
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- FALSE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->largethumb = $prefix."largethumb".$filestorename;
- tp_imagick_watermark($thumbname);
- unset($thumb);
- return TRUE;
- }
- function tp_imagick_resize($input_name, $output_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- $imgsizearray = getimagesize($input_name);
- if (!$imgsizearray) {
- return FALSE;
- }
-
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $params = tp_im_calc_resize_params($width, $height, $maxwidth, $maxheight, $square, $x1, $y1, $x2, $y2);
- if (!$params) {
- return FALSE;
- }
- $new_width = $params['new_width'];
- $new_height = $params['new_height'];
- $region_width = $params['region_width'];
- $region_height = $params['region_height'];
- $widthoffset = $params['width_offset'];
- $heightoffset = $params['height_offset'];
- try {
- $img = new Imagick($input_name);
- } catch (ImagickException $e) {
- return FALSE;
- }
- $img->cropImage($region_width, $region_height, $widthoffset, $heightoffset);
-
- $img->resizeImage($new_width, $new_height, imagick::FILTER_LANCZOS, 1);
- $img->setImagePage($new_width, $new_height, 0, 0);
- if ($img->writeImage($output_name) != TRUE) {
- $img->destroy();
- return FALSE;
- }
- $img->destroy();
- return TRUE;
- }
- function tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename) {
- $image_sizes = elgg_get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
- register_error(elgg_echo('tidypics:nosettings'));
- return FALSE;
- }
- $image_sizes = unserialize($image_sizes);
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- if (empty($image_sizes['tiny_image_width'])) {
-
- $image_sizes['tiny_image_width'] = $image_sizes['tiny_image_height'] = 60;
- }
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['tiny_image_width'],
- $image_sizes['tiny_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->thumbnail = $prefix."thumb".$filestorename;
-
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- TRUE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->smallthumb = $prefix."smallthumb".$filestorename;
-
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- FALSE);
- if (!$rtn_code) {
- return FALSE;
- }
- $file->largethumb = $prefix."largethumb".$filestorename;
- tp_im_cmdline_watermark($thumbname);
- unset($thumb);
- return TRUE;
- }
- function tp_im_cmdline_resize($input_name, $output_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- $imgsizearray = getimagesize($input_name);
- if (!$imgsizearray) {
- return FALSE;
- }
-
- $orig_width = $imgsizearray[0];
- $orig_height = $imgsizearray[1];
- $params = tp_im_calc_resize_params($orig_width, $orig_height, $maxwidth, $maxheight, $square, $x1, $y1, $x2, $y2);
- if (!$params) {
- return FALSE;
- }
- $newwidth = $params['new_width'];
- $newheight = $params['new_height'];
- $accepted_formats = array(
- 'image/jpeg' => 'jpeg',
- 'image/pjpeg' => 'jpeg',
- 'image/png' => 'png',
- 'image/x-png' => 'png',
- 'image/gif' => 'gif'
- );
-
- if (!array_key_exists($imgsizearray['mime'],$accepted_formats)) {
- return FALSE;
- }
- $im_path = elgg_get_plugin_setting('im_path', 'tidypics');
- if (!$im_path) {
- $im_path = "/usr/bin/";
- }
- if (substr($im_path, strlen($im_path)-1, 1) != "/") {
- $im_path .= "/";
- }
-
-
- $command = $im_path . "convert \"$input_name\" -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." \"$output_name\"";
- $output = array();
- $ret = 0;
- exec($command, $output, $ret);
- if ($ret == 127) {
- trigger_error('Tidypics warning: Image Magick convert is not found', E_USER_WARNING);
- return FALSE;
- } else if ($ret > 0) {
- trigger_error('Tidypics warning: Image Magick convert failed', E_USER_WARNING);
- return FALSE;
- }
- return TRUE;
- }
- function tp_im_calc_resize_params($orig_width, $orig_height, $new_width, $new_height, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- $crop = TRUE;
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
- $crop = FALSE;
- }
-
- if ($crop) {
- $region_width = $x2 - $x1;
- $region_height = $y2 - $y1;
- } else {
-
- $region_width = $orig_width;
- $region_height = $orig_height;
- }
-
- if ($square) {
-
-
- if ($crop == TRUE && $region_width != $region_height) {
- return FALSE;
- }
-
- $new_width = $new_height = min($new_width, $new_height);
-
- $region_width = $region_height = min($region_width, $region_height);
-
- if ($crop) {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $orig_width = $x2 - $x1;
- $orig_height = $orig_width;
- } else {
-
- $widthoffset = floor(($orig_width - $region_width) / 2);
- $heightoffset = floor(($orig_height - $region_height) / 2);
- }
- } else {
-
-
- if (($region_height / (float)$new_height) > ($region_width / (float)$new_width)) {
- $new_width = floor($new_height * $region_width / (float)$region_height);
- } else {
- $new_height = floor($new_width * $region_height / (float)$region_width);
- }
-
- $widthoffset = 0;
- $heightoffset = 0;
- if ($crop) {
- $widthoffset = $x1;
- $heightoffset = $y1;
- }
- }
- $resize_params = array();
- $resize_params['new_width'] = $new_width;
- $resize_params['new_height'] = $new_height;
- $resize_params['region_width'] = $region_width;
- $resize_params['region_height'] = $region_height;
- $resize_params['width_offset'] = $widthoffset;
- $resize_params['height_offset'] = $heightoffset;
- return $resize_params;
- }
|