Unable to create thumb, image is black

前端 未结 4 1531
花落未央
花落未央 2021-01-26 03:27

Am uploading Multiple image from single input and create thumb form all uploaded image on fly But when i run code i get only black image but orginal image is same as upl

4条回答
  •  孤独总比滥情好
    2021-01-26 04:07

    I think it's better to go for the mime type than the extension.

    You'll do this:

    function check_supported_type($type)
    {
        switch($type)
        {
            case "image/jpeg":
            case "image/gif":
            case "image/png":
                return true;
            default:
                return false;
        }
    }
    
    function GetMimeType($file)
    {
        //$type = mime_content_type($file); //deprecated
    
        /* //file info -> normal method, but returns wrong values for ics files..
        $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        $type = $filename.":".finfo_file($finfo, $filename);
        finfo_close($finfo);
        */
    
        $forbiddenChars = array('?', '*', ':', '|', ';', '<', '>');
    
        if(strlen(str_replace($forbiddenChars, '', $file)) < strlen($file))
            throw new \ArgumentException("Forbidden characters!");
    
        $file = escapeshellarg($file);
    
        ob_start();
        $type = system("file --mime-type -b ".$file);
        ob_clean();
    
        return $type;
    }
    

    use it like this:

    $file = "someimage.jpg";
    $mime = GetMimeType($file);
    if(check_supported_type($mime))
    {
       //do your image processing
    }
    

    hope this helps

    EDIT:

    Maybe you can take a look at my other answer: https://stackoverflow.com/a/26981319/3641016 there you'll see how to generate thumbs.

    EDIT:

    added the answer to your editet question:

    replace:

    $wm = $w/$nw;
    $hm = $h/$nw;
    $h_height = $nh/2;
    $w_height = $nw/2;
    
    if($w> $h) {
        $adjusted_width = $w / $hm;
        $half_width = $adjusted_width / 2;
        $int_width = $w / $hm;
        imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
    } else {
        imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
    }
    

    with:

    if($w > $h)
    {
        imagecopyresampled($dimg, $simg, 0,0, ($nw / $h * $w / 2 - $nw / 2),0, $nw,$nw, $h,$h);
    }
    else
    {
        imagecopyresampled($dimg, $simg, 0,0, 0,($nw / $w * $h / 2 - $nw / 2), $nw,$nw, $w,$w);
    }
    

    and everything should be ok. (if the thumb is quadratic)

提交回复
热议问题