/    Sign up×
Community /Pin to ProfileBookmark

Resizing JPEG images

How can I resize a JPEG image after a user uploads it?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@gazzwi86May 13.2008 — I dont know a way of resizing the file after you have uploaded it but you can while you upload it.

[CODE]
$error = "";

if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$imagesize = $_FILES['new_image']['size'];
$imagetype = $_FILES['new_image']['type'];
$source = $_FILES['new_image']['tmp_name'];
$target = "Uploads/".$imagename;


if ($imagetype != "image/jpeg"){
$error = "Image is not a JPEG";
}
if ($imagesize >= 5242880) {
$error = "Image exceeds the max file size - Max= 1.5 MB";
}


if ($error == "") {

move_uploaded_file($source, $target);


$imagepath ="Uploads/".$imagename;
$save = "Uploads/thumbs/".$imagename; //This is the new file you saving
$file = $imagepath; //This is the original file

list($width, $height) = getimagesize($file) ;

$modwidth = 350;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $file, 100) ;

$save = "Uploads/thumbs/".$imagename; //This is the new file you saving
$file = $imagepath; //This is the original file

list($width, $height) = getimagesize($file) ;

$modwidth = 215;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

$insert = "INSERT INTO table_name (thumbnail_path, filepath) VALUES ('$save', '$imagepath')";

mysql_select_db($database_Connect, $Connect);

mysql_query($insert, $Connect) or die(mysql_error());
}

}

}

[/CODE]
×

Success!

Help @Tabo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...