1

How to resize image before upload to dir (using image from url) ?

i want to resize image to 200x200 px, how can i do that ?

i try to upload image from url to dir upload , it's work good. But now i want to resize image before upload, how can i do that ?

<?php
if($_POST){
$url = $_POST['url'];
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;
$upload = file_put_contents("uploads/$name",file_get_contents($url));
if($upload)  echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
?>

<html>
<body>
<h3>File Upload from URL Script!</h3>
    <form action="" method="post">
        Your URL: <input type="text" name="url" />
    </form>
</body>
</html>
srimamong
  • 11
  • 2
  • possible duplicate of [php resize image on or before upload](http://stackoverflow.com/questions/12757005/php-resize-image-on-or-before-upload) – Ohgodwhy Oct 31 '14 at 05:03
  • @ Ohgodwhy not duplicate , my function use image from url . – srimamong Oct 31 '14 at 05:06
  • Since you are trying to resize an image obtained from URL, you try with saving them at some temporary folder, run resize functionality and then move it a appropriate place (wherever you want) and then delete the temporary one. – Domain Oct 31 '14 at 05:27
  • @ WisdmLabs you are correct. – srimamong Oct 31 '14 at 07:24

4 Answers4

0

You can use some api for resize the image. link

UtkarshBhavsar
  • 249
  • 3
  • 23
0

First in form tag use enctype="multipart/form-data" for upload image.

And than run this script hope this will hemp you.

if ($_POST)
{
$tmpname  = $_FILES['image']['tmp_name'];
@img_resize( $tmpname , 600 , "uploads" , "album_".$id.".jpg");
@img_resize( $tmpname , 120 , "uploads" , "album_".$id."_small.jpg");
@img_resize( $tmpname , 60 , "uploads" , "album_".$id."_maxheight.jpg", 1);
}
else
echo "No Images uploaded via POST";

function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight = 0 )
{
$save_dir     .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis        = getimagesize($tmpname);
$type        = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default:  $imorig = imagecreatefromjpeg($tmpname);
}
$x = imagesx($imorig);
$y = imagesy($imorig);

$woh = (!$maxisheight)? $gis[0] : $gis[1] ;    

if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}   
$im = imagecreatetruecolor($aw,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y))
if (imagejpeg($im, $save_dir.$save_name))
return true;
else
return false;
}
0

use enctype="multipart/form-data" in form tag

I hope this script help you.

 $path = getcwd();
        $image_name = $_FILES["file"]["tmp_name"]; // your url image name

                            function resize($path , $image_name)
                            {
                                $src = $image_name; // your url image name

                                $dest = "upload/".$image_name; // your image upload folder name

                                $size = getimagesize($src);
                                switch($size["mime"])
                                {
                                    case "image/jpeg":
                                    $source_image = imagecreatefromjpeg($src);
                                    break;
                                    case "image/gif":
                                    $source_image = imagecreatefromgif($src);
                                    break;
                                    case "image/png":
                                    $source_image = imagecreatefrompng($src);
                                    break;
                                    case "image/jpg":
                                    $source_image = imagecreatefromjpeg($src);
                                    break;

                                    default :
                                    $source_image = false;
                                    break;

                                }

                                $width = imagesx($source_image);
                                $height = imagesy($source_image);
                                /*$newwidth = 336;
                                $newheight = 195;*/
                                $newwidth = 30;
                                $newheight = 30;

                                $vertual_image = imagecreatetruecolor($newwidth,$newheight);
                                imagecopyresampled($vertual_image,$source_image,0,0,0,0,$newwidth,$newheight,$width,$height);                   

                                $rs = imagejpeg($vertual_image,$dest,100);


                            }
                            resize($path , $image_name);
mayur panchal
  • 265
  • 1
  • 16
0

Make this function

<?php
function resize($width, $height, $imgPath, $nm){
    /* Get original image x y*/
    list($w, $h) = getimagesize($_FILES[$nm]['tmp_name']);
    /* calculate new image size with ratio */
    $ratio = max($width/$w, $height/$h);
    $h = ceil($height / $ratio);
    $x = ($w - $width / $ratio) / 2;
    $w = ceil($width / $ratio);
    /* new file name */
    $path = $imgPath;
    /* read binary data from image file */
    $imgString = file_get_contents($_FILES[$nm]['tmp_name']);
    /* create image from string */
    $image = imagecreatefromstring($imgString);
    $tmp = imagecreatetruecolor($width, $height);
    //$nm = imagecreatetruecolor(400, 300);
          imagealphablending( $tmp, FALSE );
          imagesavealpha( $tmp, TRUE );
    imagecopyresampled($tmp, $image,
    0, 0,
    $x, 0,
    $width, $height,
    $w, $h);
    /* Save image */
    switch ($_FILES[$nm]['type']) {
        case 'image/jpeg':
            imagejpeg($tmp, $path, 100);
            break;
        case 'image/png':
            imagepng($tmp, $path, 0);
            break;
        case 'image/gif':
            imagegif($tmp, $path);
            break;
        default:
            exit;
            break;
    }
    return $path;
    /* cleanup memory */
    imagedestroy($image);
    imagedestroy($tmp);
}
?>

then write foloowing code before u upload file

$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
    // thumbnail sizes
    $sizes = array(200 => 200);
    $ext = strtolower(pathinfo($_FILES[$nm]['name'], PATHINFO_EXTENSION));
    if (in_array($ext, $valid_exts)) {
        /* resize image */
        foreach ($sizes as $w => $h) {
            $files[] = resize($w, $h, $imgPath ,$nm);
        }

    }
Ravi Sukhadia
  • 443
  • 6
  • 16