/    Sign up×
Community /Pin to ProfileBookmark

download image from url then resize

I have a problem, I need to take image from distant url. I use curl, then try to use a resize function. this is the code:

[CODE]$ch = curl_init($url);
$fp = fopen($source_file, ‘wb’);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);

function resize($file_input, $file_output, $w_o, $h_o, $percent = false) {
list($w_i, $h_i, $type) = getimagesize($file_input);
if (!$w_i || !$h_i) {
echo ‘Can’t get width and length’;
return;
}
$types = array(”,’gif’,’jpeg’,’png’, ‘GIF’, ‘JPEG’, ‘PNG’);
$ext = $types[$type];
if ($ext) {
$func = ‘imagecreatefrom’.$ext;
$img = $func($file_input);
} else {
echo ‘incorrect file format’;
return;
}
if ($percent) {
$w_o *= $w_i / 100;
$h_o *= $h_i / 100;
}
if (!$h_o) $h_o = $w_o/($w_i/$h_i);
if (!$w_o) $w_o = $h_o/($h_i/$w_i);
$img_o = imagecreatetruecolor($w_o, $h_o);
imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i);
if ($type == 2) {
return imagejpeg($img_o,$file_output,100);
imagedestroy($img_o);

} else {
$func = ‘image’.$ext;
return $func($img_o,$file_output);
}
}

resize($source_file, $out_s_file, 200, 200);[/CODE]

but there is some problem with it, like this Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file. And this is all about jpg file: imagecreatefromjpeg() [function.imagecreatefromjpeg]: ‘./images/dab59780be1eed54e39deb2a338c2ced.jpg’ is not a valid JPEG

the same problem with png. gif image resize ok, but there is not a dinamic gif image anymore. What I do wrong?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

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