function fix_orientation($source, $name) { $imginfo = getimagesize($source); $requiredMemory1 = ceil($imginfo[0] * $imginfo[1] * 5.35); $requiredMemory2 = ceil($imginfo[0] * $imginfo[1] * ($imginfo['bits'] / 8) * $imginfo['channels'] * 2.5); $requiredMemory = (int) max($requiredMemory1, $requiredMemory2); $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($requiredMemory > $mem_avail) { // we don't have enough memory for any manipulation // @TODO - we should only throw an error if the image needs rotating... //register_error(elgg_echo('image_orientation:toolarge')); return false; } elgg_load_library('imagine'); $name = uniqid() . $name; $tmp_location = elgg_get_config('dataroot') . 'image_orientation/' . $name; //@note - need to copy to a tmp_location as // imagine doesn't like images with no file extension copy($source, $tmp_location); try { $imagine = new Imagine(); $imagine->setMetadataReader(new ExifMetadataReader()); $autorotate = new Autorotate(); $autorotate->apply($imagine->open($tmp_location))->save($tmp_location); copy($tmp_location, $source); unlink($tmp_location); return true; } catch (Imagine\Exception\Exception $exc) { // fail silently, we don't need to rotate it bad enough to kill the script return false; } }
function put($url = '', $handle = null, $data = array()) { if (!$url) { return false; } $hash = md5($url); $site = elgg_get_site_entity(); if (!$handle) { $handle = $site->guid; } if (!empty($data)) { $thumbnails = !empty($data['thumbnails']) ? $data['thumbnails'] : array(); $icons = !empty($data['icons']) ? $data['icons'] : array(); $thumbnails = array_merge($thumbnails, $icons); if (!empty($thumbnails) && $this->config->get('cache_thumbnails')) { foreach ($thumbnails as $thumbnail_url) { $imagesize = getimagesize($thumbnail_url); if (empty($imagesize) || $imagesize[0] < $this->config->get('cache_thumb_size_lower_threshold')) { continue; } $post_max_size = elgg_get_ini_setting_in_bytes('post_max_size'); $upload_max_filesize = elgg_get_ini_setting_in_bytes('upload_max_filesize'); $max_upload = $upload_max_filesize > $post_max_size ? $post_max_size : $upload_max_filesize; $filesize = filesize($thumbnail_url); if (!$filesize || $filesize > $max_upload) { continue; } $size = $this->config->get('cache_thumb_size'); $thumb = get_resized_image_from_existing_file($thumbnail_url, $size, $size, false, 0, 0, 0, 0, true); if ($thumb) { $file = new ElggFile(); $file->owner_guid = $site->guid; $file->setFilename("scraper_cache/thumbs/{$hash}.{$handle}.jpg"); $file->open('write'); $file->write($thumb); $file->close(); $data['thumb_cache'] = time(); break; } } } } $file = new ElggFile(); $file->owner_guid = $site->guid; $file->setFilename("scraper_cache/resources/{$hash}.{$handle}.json"); $file->open('write'); $file->write(json_encode($data)); $file->close(); return $data; }
/** * Check if there is enough memory to process this image * * @param string $image_lib * @param int $requiredMemory * @return bool false = not enough memory */ function tp_upload_memory_check($image_lib, $mem_required) { if ($image_lib !== 'GD') { return true; } $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($mem_required > $mem_avail) { return false; } return true; }
/** * Get a readable byte count * * @return string */ function file_tools_get_readable_file_size_limit() { $size_units = array("B", "KB", "MB", "GB", "TB", "PB"); $i = 0; $file_size_limit = elgg_get_ini_setting_in_bytes("upload_max_filesize"); while ($file_size_limit > 1024) { $i++; $file_size_limit /= 1024; } $result = $file_size_limit . $size_units[$i]; return $result; }
$email = elgg_get_plugin_setting('email', 'contactform'); //$list1=elgg_get_plugin_setting('list1','contactform'); $formproc = new FGContactForm(); //$sim_captcha = new FGSimpleCaptcha('scaptcha'); //$formproc->EnableCaptcha($sim_captcha); //1. Add your email address here. //You can add more than one receipients. $formproc->AddRecipient($email); //<<---Put your email address here //2. For better security. Get a random tring from this link: http://tinyurl.com/randstr // and put it here $formproc->SetFormRandomKey('CnRrspl1FyEylUj'); $formproc->AddFileUploadField('photo', 'jpg,jpeg,gif,png,pdf,doc,docx,rar.zip,', 5120); // Get post_max_size and upload_max_filesize $post_max_size = elgg_get_ini_setting_in_bytes('post_max_size'); $upload_max_filesize = elgg_get_ini_setting_in_bytes('upload_max_filesize'); // Determine the correct value $max_upload = $upload_max_filesize > $post_max_size ? $post_max_size : $upload_max_filesize; $upload_limit = elgg_echo('file:upload_limit', array(elgg_format_bytes($max_upload))); if (isset($_POST['submitted'])) { if ($formproc->ProcessForm()) { system_messages(elgg_echo('contactform:thankyoumsg')); forward("mod/contactform"); // forward(elgg_get_site_url()); } } ?> <script type='text/javascript' src='scripts/gen_validatorv31.js'></script> <script>
<td><b><?php echo elgg_echo('admin:server:label:php_log'); ?> :</b></td> <td><?php echo $php_log; ?> </td> </tr> <tr class="even"> <td><b><?php echo elgg_echo('admin:server:label:mem_avail'); ?> :</b></td> <td><?php echo number_format(elgg_get_ini_setting_in_bytes('memory_limit')); ?> </td> </tr> <tr class="odd"> <td><b><?php echo elgg_echo('admin:server:label:mem_used'); ?> :</b></td> <td><?php echo number_format(memory_get_peak_usage()); ?> </td> </tr> </table>
/** * Auto-correction of image orientation based on exif data * * @param array $data */ protected function OrientationCorrection($data) { // catch for those who don't have exif module loaded if (!is_callable('exif_read_data')) { return; } $exif = exif_read_data($data['tmp_name']); $orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 0; if ($orientation != 0 || $orientation != 1) { $imageLib = elgg_get_plugin_setting('image_lib', 'tidypics'); if ($imageLib == 'ImageMagick') { // ImageMagick command line $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 .= "/"; } $filename = $data['tmp_name']; $command = $im_path . "mogrify -auto-orient {$filename}"; $output = array(); $ret = 0; exec($command, $output, $ret); } else { if ($imageLib == 'ImageMagickPHP') { // imagick php extension $rotate = false; $flop = false; $angle = 0; switch ($orientation) { case 2: $rotate = false; $flop = true; break; case 3: $rotate = true; $flop = false; $angle = 180; break; case 4: $rotate = true; $flop = true; $angle = 180; break; case 5: $rotate = true; $flop = true; $angle = 90; break; case 6: $rotate = true; $flop = false; $angle = 90; break; case 7: $rotate = true; $flop = true; $angle = -90; break; case 8: $rotate = true; $flop = false; $angle = -90; break; default: $rotate = false; $flop = false; break; } $imagick = new Imagick(); $imagick->readImage($data['tmp_name']); if ($rotate) { $imagick->rotateImage('#000000', $angle); } if ($flop) { $imagick->flopImage(); } $imagick->setImageOrientation(imagick::ORIENTATION_TOPLEFT); $imagick->writeImage($data['tmp_name']); $imagick->clear(); $imagick->destroy(); } else { // make sure the in memory image size does not exceed memory available $imginfo = getimagesize($data['tmp_name']); $requiredMemory1 = ceil($imginfo[0] * $imginfo[1] * 5.35); $requiredMemory2 = ceil($imginfo[0] * $imginfo[1] * ($imginfo['bits'] / 8) * $imginfo['channels'] * 2.5); $requiredMemory = (int) max($requiredMemory1, $requiredMemory2); $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($requiredMemory < $mem_avail) { $image = imagecreatefromstring(file_get_contents($data['tmp_name'])); $rotate = false; $flip = false; $angle = 0; switch ($orientation) { case 2: $rotate = false; $flip = true; break; case 3: $rotate = true; $flip = false; $angle = 180; break; case 4: $rotate = true; $flip = true; $angle = 180; break; case 5: $rotate = true; $flip = true; $angle = -90; break; case 6: $rotate = true; $flip = false; $angle = -90; break; case 7: $rotate = true; $flip = true; $angle = 90; break; case 8: $rotate = true; $flip = false; $angle = 90; break; default: $rotate = false; $flip = false; break; } if ($rotate) { $image = imagerotate($image, $angle, 0); imagejpeg($image, $data['tmp_name']); } if ($flip) { $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($requiredMemory < $mem_avail) { $width = imagesx($image); $height = imagesy($image); $src_x = 0; $src_y = 0; $src_width = $width; $src_height = $height; $src_x = $width - 1; $src_width = -$width; $imgdest = imagecreatetruecolor($width, $height); imagecopyresampled($imgdest, $image, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height); imagejpeg($imgdest, $data['tmp_name']); imagedestroy($imgdest); } } imagedestroy($image); } } } } }
/** * Parse and scrape a URL * * @param string $url URL * @param bool $flush Flush existing URL data * @return array|false * @throws \InvalidArgumentException */ public function parse($url, $flush = false) { if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException(__METHOD__ . ' expects a valid URL'); } if ($flush) { $this->delete($url); } else { $data = $this->get($url); if (isset($data)) { return $data; } } $response = $this->parser->request($url); if (!$response instanceof \GuzzleHttp\Psr7\Response || $response->getStatusCode() != 200) { $this->save($url, false); return false; } $post_max_size = elgg_get_ini_setting_in_bytes('post_max_size'); $upload_max_filesize = elgg_get_ini_setting_in_bytes('upload_max_filesize'); $max_upload = $upload_max_filesize > $post_max_size ? $post_max_size : $upload_max_filesize; if ((int) $response->getHeader('Content-Length') > $max_upload) { // Large images eat up memory $this->save($url, false); return false; } $data = $this->parser->parse($url); if (!$data) { $this->save($url, false); return false; } $type = elgg_extract('type', $data); switch ($type) { case 'photo': case 'image': $image = $this->saveImageFromUrl($url); if ($image instanceof ElggFile) { $data['width'] = $image->natural_width; $data['height'] = $image->natural_height; $data['filename'] = $image->getFilename(); $data['owner_guid'] = $image->owner_guid; $data['thumbnail_url'] = elgg_get_inline_url($image); } break; default: $assets = []; $thumbnails = (array) elgg_extract('thumbnails', $data, []); $icons = (array) elgg_extract('icons', $data, []); // Try 3 images and choose the one with highest dimensions $thumbnails = $thumbnails + $icons; $thumbs_parse = 0; foreach ($thumbnails as $thumbnail) { if ($thumbnail == $url) { continue; } $thumbnail = elgg_normalize_url($thumbnail); if (filter_var($thumbnail, FILTER_VALIDATE_URL)) { $asset = $this->parse($thumbnail, $flush); if ($asset) { $thumbs_parsed++; $assets[] = $asset; } } if ($thubms_parsed == 3) { break; } } $data['assets'] = array_values(array_filter($assets)); usort($data['assets'], function ($a, $b) { if ($a['width'] == $b['width'] && $a['height'] == $b['height']) { return 0; } return $a['width'] > $b['width'] || $a['height'] > $b['height'] ? -1 : 1; }); if (isset($data['assets'][0]['thumbnail_url'])) { $data['thumbnail_url'] = $data['assets'][0]['thumbnail_url']; } break; } $data = elgg_trigger_plugin_hook('parse', 'framework:scraper', array('url' => $url), $data); $this->save($url, $data); return $data; }