/    Sign up×
Community /Pin to ProfileBookmark

Resize image

I just now saving the image to database. But how to resize the image before save to database?

Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@martswiteMar 09.2009 — Hey this is the script that I use to re size images.

[code=php]//If the form has been submitted
if(isset($_POST['Submit'])){

//Assign the paths needed to variables for use in the script
$filePathFull = "assets/imgf/".$_FILES["imagefile"]["name"];
$filePathThumb = "assets/imgt/";

//Assign the files TMP name and TYPE to variable for use in the script
$tmpName = $_FILES["imagefile"]["tmp_name"];
$imageType = $_FILES["imagefile"]["type"];

//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull);
break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}

// Get information about the image
list($srcWidth, $srcHeight, $type, $attr) = getimagesize($filePathFull);

$origRatio = $srcWidth/$srcHeight;

//Create the correct file type based on imagetype
switch($type) {
case IMAGETYPE_JPEG:
$startImage = imagecreatefromjpeg($filePathFull);
break;
case IMAGETYPE_PNG:
$startImage = imagecreatefrompng($filePathFull);
break;
case IMAGETYPE_GIF:
$startImage = imagecreatefromgif($filePathFull);
break;
default:
return false;
}


//Get the image to create thumbnail from
$startImage;


$width = 200;
$height = 200;

if ($width/$height > $origRatio) {
$width = $height*$origRatio;
}else{
$height = $width/$origRatio;
}


//Create the thumbnail with true colours
$thumbImage = imagecreatetruecolor($width, $height);

//Generate the resized image image
imagecopyresampled($thumbImage, $startImage, 0,0,0,0, $width, $height, $srcWidth, $srcHeight);

//Generate a random number to append the filename.
$ran = "thumb_".rand () ;
$thumb2 = $ran.".jpg";


//global $thumb_Add_thumb;

//Create the path to store the thumbnail in.
$thumb_Add_thumb = $filePathThumb;
$thumb_Add_thumb .= $thumb2;

//Create the new image and put it into the thumbnails folder.
imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2");
}
?>[/code]


This would create a thumbnail that keeps its ratio so that it doesnt get distorted. This script saves two copies of the image. A full size and then the thumb. Save the filepath to both in your database and then call them out when you need them!
Copy linkTweet thisAlerts:
@wterauthorMar 09.2009 — Thanks martswite. Let said i have save the path of the image resize into database, how do i retrieve the image from the $filePathFull path? Or u could give me an example code so that i can easily understand.
Copy linkTweet thisAlerts:
@martswiteMar 09.2009 — Assuming you have saved the path to the database then to display the image you would do something like this.

[code=php]<?php

INSERT CONNECTION DETAILS TO DATABASE

$sql ="SELECT col_that_holds_imgthumb_path FROM table_name WHERE 1";
$query = mysql_query($sql);
$result = mysql_num_rows($query);

if($result == 0){
echo "No images in the DB";
}else{
while($row = mysql_fetch_object($query)){
echo "<img src='$row->col_that_holds_imgthumb_path' />";
}
}

?>[/code]
Copy linkTweet thisAlerts:
@MindzaiMar 10.2009 — A small tip, this code:

[code=php]
//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull);

break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull);

break;

case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull);

break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull);

break;

default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]


Can have the repetition removed and be reduced to this:

[code=php]
//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case "image/gif":
case "image/jpeg":
case "image/pjpeg":

case "image/png":
case "image/x-png":
move_uploaded_file($tmpName,$filePathFull);

break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]


Or even neater:

[code=php]
$valid = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
if (in_array($imageType, $valid) {
move_uploaded_file($tmpName,$filePathFull);

} else {
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]
Copy linkTweet thisAlerts:
@martswiteMar 10.2009 — Cheers for that Mindzai. I wasnt aware that was possible.
Copy linkTweet thisAlerts:
@knightmanJan 10.2010 — Hello


about this part:
[code=php]


$width = 200;
$height = 200;

if ($width/$height > $origRatio) {
$width = $height*$origRatio;
}else{
$height = $width/$origRatio;
}






[/code]


This is just as an example i saw on php.net, and i uderstand this will enlarge the pic if [COLOR="Red"][B]$srcWidth[/B][/COLOR] and/or [COLOR="Red"][B]$srcHeight[/B][/COLOR] is less than 200, keeping it's ratio.

What i want to do is, enlarge the image if [COLOR="Red"][B]$srcWidth[/B][/COLOR] and/or [COLOR="Red"][B]$srcHeight[/B][/COLOR] is less than 300...

BUT

shrink it if more than 800.

how can i do it?

thanks
Copy linkTweet thisAlerts:
@dk_zero-coolJan 11.2010 — http://www.webgeek.eu/?l=/en/archive/18

This is a JavaScript that does what you want. Just translate it to PHP (Takes about 1 minute).
Copy linkTweet thisAlerts:
@knightmanJan 11.2010 — http://www.webgeek.eu/?l=/en/archive/18

This is a JavaScript that does what you want. Just translate it to PHP (Takes about 1 minute).[/QUOTE]



Thanks, but i think that's not what i looking for, (not sure) So my complete scenario here is:

Step 1: Upload and save an image to a temp folder called "uploads".




Step 2: Then place a watermark on it... (the watermark is 100w x 100h)

...and copy to dir "images".


Before watermarking, copy image to a folder "temp" and resize it only IF:

if image is less than 100px on W and/or H, resize to 200px, so the watermark

wont cover the entire image. (se example below)

or if image is bigger than 800px on W and/or H, resize to 800px to avoid

having images too big to display.



*************
here's the example from:

http://php.net/manual/en/function.imagecopyresampled.php

[code=php]<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>[/code]
×

Success!

Help @wter 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.13,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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