예제 #1
1
function process_images_pages(array $file, $size)
{
    ############ Configuration ##############
    $thumb_square_size = $size;
    //Thumbnails will be cropped to 200x200 pixels
    $max_image_size = 500;
    //Maximum image size (height and width)
    $thumb_prefix = "thumb_";
    //Normal thumb Prefix
    $destination_folder = 'upload/images/';
    //upload directory ends with / (slash)
    $jpeg_quality = 90;
    //jpeg quality
    ##########################################
    // check $_FILES['ImageFile'] not empty
    if (!isset($file) || !is_uploaded_file($file['tmp_name'])) {
        die('Image file is Missing!');
        // output error when above checks fail.
    }
    //uploaded file info we need to proceed
    $image_name = $file['name'];
    //file name
    $image_size = $file['size'];
    //file size
    $image_temp = $file['tmp_name'];
    //file temp
    $image_size_info = getimagesize($image_temp);
    //get image size
    if ($image_size_info) {
        $image_width = $image_size_info[0];
        //image width
        $image_height = $image_size_info[1];
        //image height
        $image_type = $image_size_info['mime'];
        //image type
    } else {
        die("Make sure image file is valid!");
    }
    //switch statement below checks allowed image type
    //as well as creates new image from given file
    switch ($image_type) {
        case 'image/png':
            $image_res = imagecreatefrompng($image_temp);
            break;
        case 'image/gif':
            $image_res = imagecreatefromgif($image_temp);
            break;
        case 'image/jpeg':
        case 'image/pjpeg':
            $image_res = imagecreatefromjpeg($image_temp);
            break;
        default:
            $image_res = false;
    }
    if ($image_res) {
        //Get file extension and name to construct new file name
        $images_name = null;
        $image_info = pathinfo($image_name);
        $image_extension = strtolower($image_info["extension"]);
        //image extension
        $image_name_only = strtolower($image_info["filename"]);
        //file name only, no extension
        //create a random name for new image (Eg: fileName_293749.jpg) ;
        $new_file_name = $image_name_only . '_' . rand(0, 9999999999.0) . '.' . $image_extension;
        //folder path to save resized images and thumbnails
        $thumb_save_folder = $destination_folder . $thumb_prefix . $new_file_name;
        $image_save_folder = $destination_folder . $new_file_name;
        //call normal_resize_image() function to proportionally resize image
        if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {
            //call crop_image_square() function to create square thumbnails
            if (!crop_image_square($image_res, $thumb_save_folder, $image_type, $thumb_square_size, $image_width, $image_height, $jpeg_quality)) {
                return $images_name;
            }
            $images_name = $new_file_name;
            // /* We have succesfully resized and created thumbnail image
            // We can now output image to user's browser or store information in the database*/
            // echo '<div align="center">';
            // echo '<img src="uploads/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
            // echo '<br />';
            // echo '<img src="uploads/'. $new_file_name.'" alt="Resized Image">';
            // echo '</div>';
        }
        imagedestroy($image_res);
        //freeup memory
        return $images_name;
    }
}
 public function postBrandAddNew($id = "")
 {
     if (\Request::ajax()) {
         $file = "./uploads/images/";
         if (isset($_FILES["files"]) && $_FILES["files"]['tmp_name'] != "") {
             $sign_name = $_FILES["files"]["name"];
             $File_Ext = substr($sign_name, strrpos($sign_name, '.'));
             //get file extention
             $File_Name = substr($sign_name, 0, strrpos($sign_name, '.'));
             //get file extention
             $sign_tmp_name = $_FILES["files"]["tmp_name"];
             $NewFileNameSignature = preg_replace("/\\s/", "_", $File_Name) . "_" . time() . $File_Ext;
             //move_uploaded_file($tmp_name, "$uploads_dir/$name");
             move_uploaded_file($sign_tmp_name, $file . basename($sign_name));
             rename($file . basename($sign_name), $file . $NewFileNameSignature);
         }
         ############ Configuration ##############
         $thumb_square_size = 200;
         //Thumbnails will be cropped to 200x200 pixels
         $max_image_size = 500;
         //Maximum image size (height and width)
         $thumb_prefix = "thumb_";
         //Normal thumb Prefix
         $destination_folder = "./uploads/images/";
         //upload directory ends with / (slash)
         //$file =
         $jpeg_quality = 90;
         //jpeg quality
         ##########################################
         //continue only if $_POST is set and it is a Ajax request
         if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
             // check $_FILES['ImageFile'] not empty
             if (!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                 die('Image file is Missing!');
                 // output error when above checks fail.
                 exit;
             }
             //uploaded file info we need to proceed
             $image_name = $_FILES['image_file']['name'];
             //file name
             $image_size = $_FILES['image_file']['size'];
             //file size
             $image_temp = $_FILES['image_file']['tmp_name'];
             //file temp
             $image_size_info = getimagesize($image_temp);
             //get image size
             if ($image_size_info) {
                 $image_width = $image_size_info[0];
                 //image width
                 $image_height = $image_size_info[1];
                 //image height
                 $image_type = $image_size_info['mime'];
                 //image type
             } else {
                 die("Make sure image file is valid!");
             }
             //switch statement below checks allowed image type
             //as well as creates new image from given file
             switch ($image_type) {
                 case 'image/png':
                     $image_res = imagecreatefrompng($image_temp);
                     break;
                 case 'image/gif':
                     $image_res = imagecreatefromgif($image_temp);
                     break;
                 case 'image/jpeg':
                 case 'image/pjpeg':
                     $image_res = imagecreatefromjpeg($image_temp);
                     break;
                 default:
                     $image_res = false;
             }
             if ($image_res) {
                 //Get file extension and name to construct new file name
                 $image_info = pathinfo($image_name);
                 $image_extension = strtolower($image_info["extension"]);
                 //image extension
                 $image_name_only = strtolower($image_info["filename"]);
                 //file name only, no extension
                 //create a random name for new image (Eg: fileName_293749.jpg) ;
                 $new_file_name = $image_name_only . '_' . rand(0, 9999999999.0) . '.' . $image_extension;
                 //folder path to save resized images and thumbnails
                 $thumb_save_folder = $destination_folder . $thumb_prefix . $new_file_name;
                 $image_save_folder = $destination_folder . $new_file_name;
                 //call normal_resize_image() function to proportionally resize image
                 if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {
                     //call crop_image_square() function to create square thumbnails
                     // if(!crop_image_square($image_res, $thumb_save_folder, $image_type, $thumb_square_size, $image_width, $image_height, $jpeg_quality))
                     //{
                     //    die('Error Creating thumbnail');
                     // }
                     /* We have succesfully resized and created thumbnail image
                        We can now output image to user's browser or store information in the database*/
                     echo '<div align="center">';
                     // echo '<img src="/melkay/public/uploads/images/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
                     echo '<br />';
                     echo '<img src="/melkay/public/uploads/images/' . $new_file_name . '" alt="Resized Image">';
                     echo '</div>@@' . $new_file_name;
                 }
                 imagedestroy($image_res);
                 //freeup memory
             }
         }
         exit;
     }
     $validation = \Brand::validate(\Input::all());
     $input = \Input::all();
     //print_r($input);
     if ($validation->fails()) {
         return \Redirect::back()->withErrors($validation)->withInput();
     } else {
         array_forget($input, "_token");
         try {
             if ($id == "") {
                 $brand = new \Brand();
                 foreach ($input as $key => $value) {
                     $brand->{$key} = $value;
                 }
                 if ($brand->save()) {
                     \Session::put("success_message", "Record Saved");
                     return \Redirect::back();
                 } else {
                     \Session::put("error_message", "Sorry, Unexpected Error! Record Could Not Be Saved");
                     return \Redirect::back();
                 }
             } else {
                 $brand = \Brand::find($id);
                 array_forget($input, "_token");
                 foreach ($input as $key => $value) {
                     $brand->{$key} = $value;
                 }
                 if ($brand->update()) {
                     \Session::put("success_message", "Record Updated");
                     return \Redirect::back();
                 } else {
                     \Session::put("error_message", "Sorry, Unexpected Error! Record Could Not Be Saved");
                     return \Redirect::back();
                 }
             }
         } catch (ValidationException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back()->withInput()->withErrors($e->getErrors());
         } catch (\Illuminate\Database\QueryException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             exit;
         } catch (\PDOException $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             //exit;
         } catch (\Exception $e) {
             \Session::put("error_message", $e->getMessage());
             return \Redirect::back();
             //exit;
         }
     }
 }
예제 #3
0
         $image_res = false;
 }
 if ($image_res) {
     //Get file extension and name to construct new file name
     $image_info = pathinfo($image_name);
     $image_extension = strtolower($image_info["extension"]);
     //image extension
     $image_name_only = strtolower($image_info["filename"]);
     //file name only, no extension
     //create a random name for new image (Eg: fileName_293749.jpg) ;
     $new_file_name = $image_name_only . '_' . rand(0, 9999999999.0) . '.' . $image_extension;
     //folder path to save resized images and thumbnails
     $thumb_save_folder = $destination_folder . $thumb_prefix . $new_file_name;
     $image_save_folder = $destination_folder . $new_file_name;
     //call normal_resize_image() function to proportionally resize image
     if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {
         //call crop_image_square() function to create square thumbnails
         if (!crop_image_square($image_res, $thumb_save_folder, $image_type, $thumb_square_size, $image_width, $image_height, $jpeg_quality)) {
             //die('Error Creating thumbnail');
             $varible = "Error Creating thumbnail";
         }
         /* We have succesfully resized and created thumbnail image
         		 We can now output image to user's browser or store information in the database*/
         /*
          echo '<div align="center">';
         echo '<img src="uploads/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
         echo '<br />';
         echo '<img src="uploads/'. $new_file_name.'" alt="Resized Image">';
         echo '</div>';
         */
     }
예제 #4
0
function checkDimension($img_url, $size)
{
    if (!empty($img_url)) {
        $image_size_info = getimagesize($img_url);
        //get image size
        if ($image_size_info) {
            $image_width = $image_size_info[0];
            //image width
            $image_height = $image_size_info[1];
            //image height
            $image_type = $image_size_info['mime'];
            //image type
        } else {
            throw new Exception("Make sure image file is valid!");
        }
    } else {
        throw new Exception("No image url specified");
    }
    switch ($size) {
        case "SMALL":
            if ($image_width >= 230) {
                //Minimum of 230px
                $max_image_size = 250;
                $image_width = 230;
                $image_height = 230;
                $jpeg_quality = 90;
            } else {
                throw new Exception("Image do not meet the specifications for {$size}<br/>" . "Must have width and height greater or equal to 230px");
            }
            break;
        case "LARGE":
            if ($image_width > $image_height + 100 && $image_width >= 700 && $image_height >= 400) {
                //Minimum width 700 and height 400
                $max_image_size = 900;
                $image_width = 700;
                $image_height = 400;
                $jpeg_quality = 90;
            } else {
                throw new Exception("Image do not meet the specifications for {$size}<br/>" . "Must be Landscape, width >= 700px, height >= 400px");
            }
            break;
        default:
            throw new Exception("Invalid size");
    }
    switch ($image_type) {
        case 'image/png':
            $image_res = imagecreatefrompng($img_url);
            break;
        case 'image/gif':
            $image_res = imagecreatefromgif($img_url);
            break;
        case 'image/jpeg':
        case 'image/pjpeg':
        case 'image/jpg':
            $image_res = imagecreatefromjpeg($img_url);
            break;
        default:
            $image_res = false;
    }
    //call normal_resize_image() function to proportionally resize image
    normal_resize_image($image_res, $img_url, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality);
}