/    Sign up×
Community /Pin to ProfileBookmark

making thumbnails

Hmm, it’s been a while since I dropped in here, haven’t had many problems with my PHP scripts. Hah so much for that streak ?

I’ve got a folder with about 150 pictures in it which are quite large. Since I don’t really want to go through each one separately and make thumbnails of each, I want to make a sort of one-time script that will go through the entire folder and create a smaller thumbnail of each picture. I’ve got most of it down, but I’m just not sure how to resize it.

[code=php]<?php

header(“Content-type: image/jpeg”);

if($handle = opendir(‘../pics’)){
while(false !== ($file = readdir($handle))){
if(is_file($file) && $file != “cleanup.php”){
$pics[] = $file;
}
}
closedir($handle);
}

$num = count($pics);

for($i = 0; $i <= ($num – 1); $i++){

$size = getimagesize($pics[$i]);

if($size[0] > 200){
$width = 200;
$factor = $size[0] / $width;
$height = $size[1] / $factor;
}
elseif($size[1] > 150){
$height = 150;
$factor = $size[1] / $height;
$width = $size[0] / $factor;
} else {
$width = $size[0];
$height = $size[1];
}

$image = imagecreatefromjpeg($pics[$i]);

imagejpeg($image);

}

?>[/code]

As you can see, the script puts all the image names into an array (which I’m going to use for some other stuff later), and then goes through that array and figures out the width and height for each (I also want to keep the the image size ratio, basically keeping the images 200×150 or smaller, but I don’t want distorted thumbnails). And then here’s where I get lost. I don’t exactly want to create an image to display, I want to create an actual file without displaying it if possible – but I don’t know how to resize it to $width and $height. Can anyone help me? Thanks in advance. ?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

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