Jump to content

upload image


Gazz1982

Recommended Posts

Im using this code to upload an image from a form

 

<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (move_uploaded_file($_FILES['upfile']['tmp_name'],
$uploadfile)) {
   print("File upload success");
} else {
   print("Failed");
}

?>

 

 

I want add so that the file is also sent to another file

 

This file needs to be a thumb nail so 100px wide

 

is this possible?

 

 

Link to comment
Share on other sites

I'm trying to upload an image from 1 file to to locations how can I combine these 2 scripts together

 

<?php
$uploaddir1 = "uploads/";
$uploadfile1 = $uploaddir1 . $_FILES['upfile']['name'];
if (move_uploaded_file($_FILES['upfile']['tmp_name'],
$uploadfile1)) {
   print("File upload success");
} else {
   print("Failed");
}
?>

<?php
$uploaddir2 = "uploads/thumbs/";
$uploadfile2 = $uploaddir2 . $_FILES['upfile']['name'];
if (move_uploaded_file($_FILES['upfile']['tmp_name'],
$uploadfile2)) {
   print("<br />Thumbnail upload success");
} else {
   print("<br />Thumbnail Failed");
}

?>

Link to comment
Share on other sites

I don't know if it wil help you but I have done what you are looking for

 

I've attached my script so you can use the whole thing or grab what you want.

 

When you upload a file, it uploads to a specific folder and it resizes it to a smaller image and a thumbnail image and it puts them in 2 seperate folders. So you end up with the original image, a medium resized image and a thumbnail

 

[attachment deleted by admin]

Link to comment
Share on other sites

this if for a jpeg but you can do if/else statements for other  image file s

 

 

if(isset($_FILES['upfile'])) {

 

$uploaddir1 = $_SERVER['DOCUMENT_ROOT'] ."/uploads/";

$uploadfile1 = $uploaddir1 . $_FILES['upfile']['name'];

$upload = $_FILES['upfile']['tmp_name'];

if(file_exists($_FILES['upfile']['tmp_name'])) {

 

  $err = move_uploaded_file($upload, $uploadfile1);

 

  if($err == FALSE){

    echo "File upload success";

    }

    else{

    echo "Failed";

    }

}

}

 

$imgpath = $uploadfile1;

$thumbpath = $uploaddir1 . "/thumbs/" . $_FILES['upfile']['name'];

$img = imagecreatefromjpeg($imgpath);

 

$percent = 0.20;

list($imgwidth,$imgheight) = getimagesize($imgpath);

 

if($imgwidth > $imgheight){

$newimgwidth = 100;

$newimgheight = (100/$width) * $imgheight;

}

else {

$newimgheight = 100;

$newimgwidth = (100/$imgheight) * $imgwidth;

}

 

$newimgheight = $imgheight * $percent;

$newimage = imagecreatetruecolor($newimgwidth, $newimgheight);

 

imagecopyresampled($newimage, $img,0,0,0,0,$newimgwidth, $newimgheight,

$imgwidth, $imgheight);

 

imagejpeg($newimage, $thumbpath,100);

 

$im = imagecreatefromjpeg($thumbpath);

Link to comment
Share on other sites

Forget my previous post. I tried to write it off the top of my head without testing - it is buggy. This one I tested.  the $flag variable sets the thumbnail width and based off that the height will be set so the thumb looks proportional.

The script works for jpegs, but you can add if/else conditions to allow it to create any other image format.

------------------------------------------------------

<?php

if(isset($_FILES['upfile'])) {

 

$uploaddir1 = $_SERVER['DOCUMENT_ROOT'] ."/uploads/";

$uploadfile1 = $uploaddir1 . $_FILES['upfile']['name'];

$upload = $_FILES['upfile']['tmp_name'];

 

if(file_exists($_FILES['upfile']['tmp_name'])) {

 

  $err = move_uploaded_file($upload, $uploadfile1);

    if($err == FALSE){

      echo "File upload failed";

      exit();

      }

 

$imgpath = $uploadfile1;

$thumbpath = $uploaddir1 . "/thumbs/" . $_FILES['upfile']['name'];

$img = imagecreatefromjpeg($imgpath);

 

list($imgwidth,$imgheight) = getimagesize($imgpath);

echo $imgwidth . "<br>";

$flagwidth = 100;

$percent = round(($flagwidth/$imgwidth) * 100) * .01;

$newimgwidth = $imgwidth * $percent;

$newimgheight = $imgheight * $percent;

$newimgheight = $imgheight * $percent;

$newimage = imagecreatetruecolor($newimgwidth, $newimgheight);

 

imagecopyresampled($newimage, $img,0,0,0,0,$newimgwidth, $newimgheight,

$imgwidth, $imgheight);

 

imagejpeg($newimage, $thumbpath,100);

 

$im = imagecreatefromjpeg($thumbpath);

}

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.