• 09-03-2023, 16:27:38
    #1
    Merhaba, Aşağıdaki kod bloğu ile uzun zamandır çalışıyordum fakat dinamik bir yapıda değil. Örnek olarak 1920x1080 olan resmi orginal ve 2 tane thumb olmak üzere upload işlemi gerçekleştiriyorum. Fakat 400x250 ve 120x100 olarak dinamik olmayan bir yapı var. ve fotoğraflar yayılmış bir şekilde çıkıyor ve ek olarak telefon dikey olarak çekildiğinde 1080x1920 gibi terz çözünürlük oluyor onu da yatay olarak çeviriyor dolayısı ile her türlü yayılmış bozulmuş bir görüntü ortaya çıkıyor. Acaba bunun dinamik bir çözümü var mı?. Şimdiden teşekkür ediyorum.

    <?php
    function correctImageOrientation($filename)
    {
    $exif = exif_read_data($filename);
    if ($exif && isset($exif['Orientation'])) {
    $orientation = $exif['Orientation'];
    if ($orientation != 1) {
    $img = imagecreatefromjpeg($filename);
    $deg = 0;
    switch ($orientation) {
    case 3:
    $deg = 180;
    break;
    case 6:
    $deg = 270;
    break;
    case 8:
    $deg = 90;
    break;
    }
    if ($deg) {
    $img = imagerotate($img, $deg, 0);
    }
    imagejpeg($img, $filename, 95);
    }
    }
    }
    
    
    function scale_image($image,$target,$target_width,$target_height)
    {
    
    if(!empty($image))
    {
    $source_image = null;
    
    
    $exploded = explode('.',$image);
    $ext = $exploded[count($exploded) - 1];
    
    if (preg_match('/jpg|jpeg/i',$ext))
    $source_image = imagecreatefromjpeg($image);
    else if (preg_match('/png/i',$ext))
    $source_image = imagecreatefrompng($image);
    else if (preg_match('/gif/i',$ext))
    $source_image = imagecreatefromgif($image);
    else if (preg_match('/bmp/i',$ext))
    $source_image = imagecreatefrombmp($image);
    
    
    
    $source_imagex = imagesx($source_image);
    $source_imagey = imagesy($source_image);
    
    
    
    $dest_imagex = $target_width;
    $dest_imagey = $target_height;
    
    
    
    $image2 = imagecreatetruecolor($dest_imagex, $dest_imagey);
    imagecopyresampled($image2, $source_image, 0, 0, 0, 0,
    $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
    
    imagejpeg($image2, $target, 65);
    correctImageOrientation($target);
    }
    }
    
    
    
    if(isset($_FILES["uploaded_file"]["name"]))
    {
    $name = $_FILES["uploaded_file"]["name"];
    $tmp_name = $_FILES["uploaded_file"]["tmp_name"];
    $error = $_FILES["uploaded_file"]["error"];
    
    
    
    
    if(!empty($name)){
    $location = '../appapibizifarkedin/resimler/';
    if(!is_dir($location))
    mkdir($location);
    if(move_uploaded_file($tmp_name,$location.$name)){
    $imageFileType = strtolower(pathinfo($location.$name,PATHINFO_EXTENSION));
    scale_image($location.$name,
    substr($location.$name,0,strrpos($location.$name, ".")) . "_120x100" .  "." . $imageFileType, 640, 480);
    scale_image($location.$name,substr($location.$name,0,strrpos($location.$name, ".")) . "_400x250" .  "." . $imageFileType, 1920, 1080);
    echo json_encode(['status' => 'ok']);
    }else{
    echo json_encode(['status' => 'fail']);
    }
    }else{
    echo json_encode(['status' => 'errorfile']);
    }
    }
    
    
    ?>
  • 09-03-2023, 18:17:25
    #2
    https://github.com/claviska/SimpleImage

    basit ve kullanımı kolay, orientation yapılabilen ve istenilen boyutlarda çıktı verebilen bi sınıf.
    bestFit() fonksiyonunu kullanırsanız resim bozulmadan en uygun şekilde belirlediğiniz boyut için resmi eğip bükmeden boyutlandırır.
  • 10-03-2023, 10:29:15
    #3
    ebasit adlı üyeden alıntı: mesajı görüntüle
    https://github.com/claviska/SimpleImage

    basit ve kullanımı kolay, orientation yapılabilen ve istenilen boyutlarda çıktı verebilen bi sınıf.
    bestFit() fonksiyonunu kullanırsanız resim bozulmadan en uygun şekilde belirlediğiniz boyut için resmi eğip bükmeden boyutlandırır.
    Öncelikle çok teşekkür ederim. Kodumu aşağıda ki gibi güncelledim, oriantation ve mb olarak boyut küçültmesini yaptı. Fakat koddaki aşağıdaki kod da görüldüğü gibi _480x360 ve _1024x768 boyutlarında iki tane resim boyutlandırmasını yaptırmaya çalışıyorum. isim olarak ikisi de yükleniyor fakat boyutları ve çözünürlüklerini aynı yapıyor.
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require_once('claviska/SimpleImage.php');
    if(isset($_FILES["uploaded_file"]["name"])) {
        $name = $_FILES["uploaded_file"]["name"];
        $tmp_name = $_FILES["uploaded_file"]["tmp_name"];
        $error = $_FILES["uploaded_file"]["error"];
        if(!empty($name)){
            $location = '../appapibizifarkedin/resimler/';
            if(!is_dir($location)) {
                mkdir($location);
            }
            if(move_uploaded_file($tmp_name, $location.$name)){
                $imageFileType = strtolower(pathinfo($location.$name, PATHINFO_EXTENSION));
                $simpleImage = new \claviska\SimpleImage();
                $simpleImage->fromFile($location.$name)
                            ->autoOrient()
                            ->bestFit(480, 360, ['aspectRatio' => true])
                            ->toFile(substr($location.$name, 0, strrpos($location.$name, ".")) . "_480x360." . $imageFileType)
                            ->bestFit(1024, 768, ['aspectRatio' => true])
                            ->toFile(substr($location.$name, 0, strrpos($location.$name, ".")) . "_1024x768." . $imageFileType);
                echo json_encode(['status' => 'ok']);
                exit;
            } else {
                echo json_encode(['status' => 'fail']);
                exit;
            }
        } else {
            echo json_encode(['status' => 'errorfile']);
            exit;
        }
    }
    ?>
    Edit: Aşağıdaki kodu çalışanla güncelliyorum. Tekrardan teşekkür ederim bu kütüphaneyi önerdiğiniz için. Çözünürlükleri ayrı ayrı değişlenle yaptığımda çalıştı. Çalışan kod aşağıdadır. İşine yarayan çıkar diye ekledim. (Not: PHP 8 altında hata alınıyor. En az 8 olması gerek)
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require_once('claviska/SimpleImage.php');
    if(isset($_FILES["uploaded_file"]["name"])) {
        $name = $_FILES["uploaded_file"]["name"];
        $tmp_name = $_FILES["uploaded_file"]["tmp_name"];
        $error = $_FILES["uploaded_file"]["error"];
        if(!empty($name)){
            $location = '../appapibizifarkedin/resimler/';
            if(!is_dir($location)) {
                mkdir($location);
            }
            if(move_uploaded_file($tmp_name, $location.$name)){
                $imageFileType = strtolower(pathinfo($location.$name, PATHINFO_EXTENSION));
                $simpleImage = new \claviska\SimpleImage();
                $simpleImage->fromFile($location.$name)
                            ->autoOrient()
                            ->bestFit(640, 480, ['aspectRatio' => true])
                            ->toFile(substr($location.$name, 0, strrpos($location.$name, ".")) . "_400x250." . $imageFileType);
                $simpleImage = new \claviska\SimpleImage();
                $simpleImage->fromFile($location.$name)
                            ->autoOrient()
                            ->bestFit(480, 360, ['aspectRatio' => true])
                            ->toFile(substr($location.$name, 0, strrpos($location.$name, ".")) . "_120x100." . $imageFileType);
                echo json_encode(['status' => 'ok']);
                exit;
            } else {
                echo json_encode(['status' => 'fail']);
                exit;
            }
        } else {
            echo json_encode(['status' => 'errorfile']);
            exit;
        }
    }
    ?>