/    Sign up×
Community /Pin to ProfileBookmark

How to resize image with PHP code?

I have script to show image and need to know how to resize with code such image?

So, images are big like: 2048×1536 (this size should be original on server)

I would like to transorm with PHP or other code to 269×202

Need help.

Regards and thank you?

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@spliffMay 30.2007 — uuuuuuuh doenst it just work with specifying width and height within <img > ??

or are you pulling a bit of html out of a database
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — Yes, I have put, it works

thank you
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — You want to resize 2048x1536 image with just setting 269x202 dimensions within img tag? No.. ? that is so evil idea ? ? ? because browser will still download the original large image in the situation when just a small image is needed.

Use rather some php resize function like this

http://fundisom.com/phparadise/php/image_handling/resize_offsite_image#
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — thank you. Probably this is reason that I have problem with download (in Mozilla is seen this as it would be broken image for second).

I have code in javascript like:
[code=php]
document.getElementById('selimage').innerHTML = "<img alt='My products' width='269' height='202' src='" + image + "'>";
[/code]


Is possible to read from particular directory images and show in Javascript variable?

How to do correct code?

I have some code like:
[code=php]
//set new height
$size = 150;
$src = imagecreatefromstring ($data);
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;

//start resizing
if ($height <= $size)
{
$new_w = $width;
$new_h = $height;
}
else {
$new_h = $size;
$new_w = abs($new_h / $aspect_ratio);
}
[/code]
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — I don't know why you are using javascript there, but if you want to display small images something like thumbnails gallery use script like this...


// this is the original image

<img src="<?php echo 'image2048x1536.jpg'?>">

// this is how to display small image

<img src="<?php echo 'thumbnail.php?name=image2048x1536.jpg'?>">
[/QUOTE]
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — I have seen that it takesone second to show image in Javascript when user goes over link.

this is probably because I have very large image and in html code change to small one line width=''172''

this will not happen I assume when I make them smaller with fast download. right?
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — Yes... As i said ... use script for resizing like this http://fundisom.com/phparadise/php/image_handling/resize_offsite_image# modify this script to open image listed in $_GET['name'] variable, and your problem is gone.
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — How to read images from server from particular directory?
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — this way


fopen($_SERVER['DOCUMENT_ROOT'].'/imagesfolder/'.$_GET['name'], 'rb')
[/QUOTE]
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — What is 'rb' specification?
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — rb stands for [I]rubbish[/I] or [I]read binary[/I] ?

this is PHP fopen manual
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — is obligation to have?
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with rn characters.
Copy linkTweet thisAlerts:
@toplisekauthorMay 30.2007 — Why is in code $size = 150; ?

Where is defined new size as I need to want to resize 2048x1536 image with setting 269x202 dimensions
Copy linkTweet thisAlerts:
@redhatlookMay 30.2007 — It seems that $size is a new height of the image so in your case 202 and width will be based on image dimension. Play around with it
Copy linkTweet thisAlerts:
@bokehMay 30.2007 — As usual Toplisek, your posts are as captivating as your website.
×

Success!

Help @toplisek 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.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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