/    Sign up×
Community /Pin to ProfileBookmark

Inserting my funtion to rotate an image

This code works correctly when it is all put together, except for the image size, it should resize to 50%, but when I add in my function, it doesn’t resize it, but rotating it works. If I remove the function, then resizing does work, so where and how would I insert it?

OK… I have this code:

[code=php]<?php
// The file
session_start();
$img_id = $_GET[‘image_id’];
include”db.php”;
$img = mysql_query(“SELECT * FROM files where file_id=’$img_id'”)or die(mysql_query());
$imgs = mysql_fetch_array($img);

$filename = “users/”.$_SESSION[‘user’].”/$imgs[file_name]”;
#$filename = “users/ryan/screen2.jpg”;
//$filename = ‘images/user_images/geoff.jpg’;

$percent = .5;

function getext($file) {
$pos = strrpos($file,’.’);
$str = substr($file, $pos);
return strtolower($str);
}

// Content type
if(getext($filename)==’.jpg’){
header(‘Content-type: image/jpeg’);
}elseif(getext($filename)==’.gif’){
header(‘Content-type: image/gif’);
}elseif(getext($filename)==’.png’){
header(‘Content-type: image/png’);
}

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);

if(getext($filename)==’.jpg’){
$source = imagecreatefromjpeg($filename);
#$source = rotate($filename,$_GET[‘degrees’]);
//$thumb = rotate($source,$_GET[‘degrees’]);
}elseif(getext($filename)==’.gif’){
$source = imagecreatefromgif($filename);
}elseif(getext($filename)==’.png’){
$source = imagecreatefrompng($filename);
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
if(getext($filename)==’.jpg’){
imagejpeg($thumb);
}elseif(getext($filename)==’.gif’){
imagegif($thumb);
}elseif(getext($filename)==’.png’){
imagepng($thumb);
}
?>[/code]

I want to add this function, so I add it to the top.

[code=php]function rotate($filerotate,$rotationamount){
// File and rotation
$filename = $filerotate;
$degrees = $rotationamount;

// Content type
header(‘Content-type: image/jpeg’);

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
return imagejpeg($rotate);
}[/code]

My question is… Where do I add this:
[b]$thumb = rotate($source,$_GET[‘degrees’]);[/b]

I don’t know if [b]$source[/b] or [b]$thumb[/b] is the correct variable to use, but [b]$_GET[‘degrees’][/b] is required

If you need more Information, please ask.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@phpshift2Dec 22.2006 — Not sure if it will work, but this is probably my best shot:

[code=php]
<?php
// The file
session_start();
$img_id = $_GET['image_id'];
include"db.php";
$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());
$imgs = mysql_fetch_array($img);

$filename = "users/".$_SESSION['user']."/$imgs[file_name]";
#$filename = "users/ryan/screen2.jpg";
//$filename = 'images/user_images/geoff.jpg';

$percent = .5;

function getext($file) {
$pos = strrpos($file,'.');
$str = substr($file, $pos);
return strtolower($str);
}

// Content type
if(getext($filename)=='.jpg'){
header('Content-type: image/jpeg');
}elseif(getext($filename)=='.gif'){
header('Content-type: image/gif');
}elseif(getext($filename)=='.png'){
header('Content-type: image/png');
}

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);

if(getext($filename)=='.jpg'){
$source = imagecreatefromjpeg($filename);
#$source = rotate($filename,$_GET['degrees']);
//$thumb = rotate($source,$_GET['degrees']);
}elseif(getext($filename)=='.gif'){
$source = imagecreatefromgif($filename);
}elseif(getext($filename)=='.png'){
$source = imagecreatefrompng($filename);
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
}
// Rotate
imagerotate($source, $_GET['degrees'], 0);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);


// Output
if(getext($filename)=='.jpg'){
imagejpeg($thumb);
}elseif(getext($filename)=='.gif'){
imagegif($thumb);
}elseif(getext($filename)=='.png'){
imagepng($thumb);
}
?>
[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorDec 22.2006 — nope... That didn't work.
×

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 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,
)...