/    Sign up×
Community /Pin to ProfileBookmark

Can someone help me modify this code? I want it to create a thumbnail of the original image, but I don’t want to save the original image, just the thumbnail, and delete the original image.

[code=php]function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality = 60){
$details = getimagesize(“$imageDirectory/$imageName”) or die(‘<div class=”container”>
<div class=”content”>
Please only upload images.
<p>
<a class=”bod” href=”user.php?login=upload”>Uploads</a>
</p>
</div>
</div>’);
$type = preg_replace(‘@^.+(?<=/)(.+)$@’, ‘$1’, $details[‘mime’]);
eval(‘$srcImg = imagecreatefrom’.$type.'(“$imageDirectory/$imageName”);’);
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
eval(‘image’.$type.'($thumbImg, “$thumbDirectory/$imageName”‘.(($type==’jpeg’)?’, $quality’:”).’);’);
imagedestroy($srcImg);
imagedestroy($thumbImg);
}
foreach ($_FILES[“pictures”][“error”] as $key => $error)
{
if($error === 0)
{
$filename = time().$_FILES[“upload_img”][“name”][$key];
move_uploaded_file($_FILES[“upload_img”][“tmp_name”][$key], “images/user_images/$filename”);
createThumbnail(“images/user_images”, “$filename”, “images/user_images”, 350);
mysql_query(“INSERT INTO images(`userid`, `filename`) VALUES (‘$row[userid]’, ‘$filename’)”);
}
}[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chesemonkylomaSep 17.2006 — have you done

unlink($imageDirectory/$imageName);


?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 17.2006 — I actually changed my mind of what I am looking for.

I now would like to keep the original, but resize it to 350px, and make a small thumbnail of that, so that it has a width of 100px.
Copy linkTweet thisAlerts:
@chesemonkylomaSep 17.2006 — well, i'm not good with the gd library, all i knew was about unlink ?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorSep 18.2006 — I sorta figured somthing out, but I cant get it to work.

The image that will be put into resize.php will change everytime.

It works if I place a name like time.jpg in for $imgname, but I can't get it to work using this method, how can I put the file name in the src, so it can resize that image?

I have tried $_POST[img], but that just gives me nothing, and $_POST[img] holds a value like xxx.jpg

[code=php]echo'<img alt="'.$_POST['img_id'].'" src="resize.php">';[/code]

[B]resize.php[/B]
[code=php]<?php
// The file
$filename = "images/user_images/$imgname";

// Set a maximum height and width
$width = 100;
$height = 20000;

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

// 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
$img = imagejpeg($image_p, null, 100);
?> [/code]
Copy linkTweet thisAlerts:
@paoloooDec 21.2006 — Bai!, ?

Hello Try this one. Hope i will work.

<?php

if ($_POST[submit]!="") {

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality = 60){

$details = getimagesize("$imageDirectory/$imageName");

if ($details === false){

unlink("$imageDirectory/$imageName");

DIE('<div class="container">

<div class="content">

Please only upload images.

<p>

<a class="bod" href="'.$_SERVER[PHP_SELF].'">Uploads</a> #user.php?login=upload

</p>

</div>

</div>');

}

$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcImg);
imagedestroy($thumbImg);

}

foreach ($_FILES["pictures"]["error"] as $key => $error)

{

if($error === 0)

{

$filename = time().$_
FILES["pictures"]["name"][$key];

$name = $_FILES["pictures"]["name"][$key];

move_uploaded_file($_
FILES["pictures"]["tmp_name"][$key], "images/user_images/$filename");

createThumbnail("images/user_images", "$filename", "images/user_images", 350);

mysql_query("INSERT INTO images(userid, filename) VALUES ('$row[userid]', '$filename')");

}

}

}

?>

<form action="<?=$_SERVER[PHP_SELF]?>" enctype="multipart/form-data" method="post">

<input type="file" name="pictures[]"><br/>

<input type="submit" value="Upload" name="submit">

</form>

------------- ?



Can someone help me modify this code? I want it to create a thumbnail of the original image, but I don't want to save the original image, just the thumbnail, and delete the original image.

[code=php]function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality = 60){
$details = getimagesize("$imageDirectory/$imageName") or die('<div class="container">
<div class="content">
Please only upload images.
<p>
<a class="bod" href="user.php?login=upload">Uploads</a>
</p>
</div>
</div>');
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcImg);
imagedestroy($thumbImg);
}
foreach ($_FILES["pictures"]["error"] as $key => $error)
{
if($error === 0)
{
$filename = time().$_FILES["upload_img"]["name"][$key];
move_uploaded_file($_FILES["upload_img"]["tmp_name"][$key], "images/user_images/$filename");
createThumbnail("images/user_images", "$filename", "images/user_images", 350);
mysql_query("INSERT INTO images(userid, filename) VALUES ('$row[userid]', '$filename')");
}
}[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@paoloooDec 21.2006 — Bai! Hello ?

I hope this can help you...

[code=php] echo'<img alt="'.$_POST['img_id'].'" src="resize.php?imgFilename=1166734003wallpaper3.jpg">'; [/code]

[code=php]
<?php
$imgname = $_GET[imgFilename];
// The file
$filename = "images/user_images/$imgname";

// Set a maximum height and width
$width = 100;
$height = 20000;

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

// 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
$img = imagejpeg($image_p, null, 100);
?> [/code]


TRY it!.. Tell me if there's something wrong.. ?
×

Success!

Help @The_Little_Guy 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.19,
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,
)...