Unable to create thumb, image is black

前端 未结 4 1529
花落未央
花落未央 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:15

    This is my code for uploading multiple files and cropping and cropping them.

    It uploads the image to a folder, then picks the image, crops it , re-uploads the cropped image and deletes the original image.

    Am sure you can play around with this

    This is the php code

    if(isset($_POST['upload_gal']))
    {
    
    $fk_id = $_POST['fk_id'];
    
    
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];
    
    
       if($file_type=='image/jpeg'||$type=='image/gif'||$type=='image/bmp'||$type=='image/png')
                 {
        $image_info = getimagesize($_FILES["files"]["tmp_name"][$key]);
        $image_width = $image_info[0];
        $image_height = $image_info[1];
    
                     $desired_dir="brand_images/";
    
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0755);        // Create directory if it does not exist
            }
    
    
    
    
                $locationing="brand_images/$file_name";
                move_uploaded_file($file_tmp,$locationing);
    
    
                 $image = imagecreatefromstring(file_get_contents("brand_images/$file_name"));
            $rand = rand(111,43943749739349343);
            $filename = "brand_images/$rand-33$file_name";
    
             if($image_width >= 840 && $image_height >= 680)
            {
            $thumb_width = 1200;
            $thumb_height = 700;
            }else{
            $thumb_width = 800;
            $thumb_height = 533;
            }
    
            $width = imagesx($image);
            $height = imagesy($image);
    
            $original_aspect = $width / $height;
            $thumb_aspect = $thumb_width / $thumb_height;
    
            if ( $original_aspect >= $thumb_aspect )
            {
               // If image is wider than thumbnail (in aspect ratio sense)
               $new_height = $thumb_height;
               $new_width = $width / ($height / $thumb_height);
            }
            else
            {
               // If the thumbnail is wider than the image
               $new_width = $thumb_width;
               $new_height = $height / ($width / $thumb_width);
            }
    
            $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
    
            // Resize and crop
            imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
            imagejpeg($thumb, $filename, 80);
    
                                                           move_uploaded_file($tmp_name, $filename);
    
    
    mysql_query("INSERT INTO gallery VALUES('','brand_images/$rand-33$file_name','$fk_id')");
    
            echo"";
            }
    

    This is the html

    Upload a new image


    Upload image:

    If image is larger than 800x533, the image would be cropped

提交回复
热议问题