PHP - resize image problem

Asked By Olusegun A.
28-Aug-10 11:54 AM
Hello friends,

please am having problem with my image resize code the code is as below;

$originalImage = $_FILES["file"]["name"];
$toWidth = 300;
$toHeight = 300;

function resizeImage($originalImage,$toWidth,$toHeight){

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}

// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

return $imageResized;
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $imageResized);
$path2= "../upload/" . $imageResized;

but the picture is not resized, and if i use

$image = resizeImage()
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $image);
$path2= "../upload/" . $image;

no picture will be uploaded to my upload folder.

please what can i do or which other lesser stress way can i resize my picture.
  Super Man replied to Olusegun A.
28-Aug-10 12:03 PM
01.i am not good in php.
02.but provide you one sample code for that:
03.  
04.function makeimage($filename, $newfilename, $path, $newwidth, $newheight) { 
05.  
06.  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . ) 
07.  $image_type = strstr($filename'.'); 
08.  
09.  //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION 
10.    switch($image_type) { 
11.      case '.jpg'
12.        $source = imagecreatefromjpeg($filename); 
13.        break
14.      case '.png'
15.        $source = imagecreatefrompng($filename); 
16.        break
17.      case '.gif'
18.        $source = imagecreatefromgif($filename); 
19.        break
20.      default
21.        echo("Error Invalid Image Type"); 
22.        die
23.        break
24.      
25.     
26.  //CREATES THE NAME OF THE SAVED FILE 
27.  $file = $newfilename . $filename
28.     
29.  //CREATES THE PATH TO THE SAVED FILE 
30.  $fullpath = $path . $file
31.  
32.  //FINDS SIZE OF THE OLD FILE 
33.  list($width$height) = getimagesize($filename); 
34.  
35.  //CREATES IMAGE WITH NEW SIZES 
36.  $thumb = imagecreatetruecolor($newwidth$newheight); 
37.  
38.  //RESIZES OLD IMAGE TO NEW SIZES 
39.  imagecopyresized($thumb$source,  0,  0,  0,  0,  $newwidth$newheight$width$height); 
40.  
41.  //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100 
42.  imagejpeg($thumb$fullpath,  60); 
43.  
44.  //CREATING FILENAME TO WRITE TO DATABSE 
45.  $filepath = $fullpath
46.     
47.  //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION 
48.  return $filepath
49.  
50.
51.  
52.?>

you can also visit this good link:

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

 

Create New Account
help
what is the error in that function , please send me the correct format of this function create function EvenOdd ( @num int ) return varchar ( 20 ) as begin declare @string varchar ( 20 ) if ( @num % 2 o ) set @string = 'odd' else set @string = 'even' return ( @string ) end Hi, check this. . create function EvenOdd ( @num int ) returns varchar ( 20 ) as begin declare @string varchar ( 20 ) if ( @num % 2 I suppose you are comparing with o (alphabet o) instead of number 0. . . Try this. . . create function EvenOdd ( @num int ) return varchar ( 20 ) as begin declare @string varchar ( 20 ) if ( @num
SQL in table-valued UDF: How to (or equivalent) SQL Server If I try to create this function, I get an error: CREATE FUNCTION udf_test() RETURNS @DATER TABLE ( DATE_THEN datetime - -collate SQL_Latin1_General_CP850_BIN2 NOT NULL ) AS BEGIN DECLARE @SQL_STATMENT_TX nvarchar Procedure udf_test, Line 10 Invalid use of a side-effecting operator 'EXECUTE STRING' within a function. These three lines execute fine: DECLARE @SQL_STATMENT_TX nvarchar(4000) SET @SQL_STATMENT_TX = 'SELECT getdate() where ''x'' = ''x''' EXEC (@SQL_STATMENT_TX) Why would I want to make such a stupid function? This is just an example, not the actual function. I am working with vendor product (re-design is out of the question). I need
Help with Function please Hello, I have this function CREATE FUNCTION dbo.Mid (@String varchar(4000), @startIndex int, @length int = null) RETURNS varchar(4000) AS BEGIN when i execute it like this exec dbo.Mid('5', 1, 1) i get the error " Line 1: Incorrect syntax near '5'." how can i fix this? i wan tto be to use Mid in dynamic select queries. thanks, burak Since it is a user-defined function, you cannot use the EXECUTE command to execute it. You can only include it in try select db.Mid('5', 1, 1) as [Alias Name for your returned value] since function always returns some value, it cannot be used with exec instead to be used only with select / print. Create New Account
sql function HAI, How to create function in sql. And how to use that in our sql query. if it returns any how to get that value in sql. Thanks in advance HI syntax -Transact-SQL Scalar Function Syntax CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [ , . . .n ] ] ) RETURNS return_data_type [ WITH <function_option> [ , . . .n ] ] [ AS ] BEGIN function_body RETURN scalar_expression END [ ; ] See this example CREATE FUNCTION whichContinent (@Country nvarchar(15)) RETURNS varchar(30) AS BEGIN declare @Return varchar(30) select @return
Passing of variables C++ / VB All, I am trying to call a variable from a function. Here is how my script is setup. var1 = value one(var1) write var2 Function one(var1) two(var1) End Function Function two(var1) three(var1) End Function Function three(var1) four(var1) End Function Function four(var1) var2 = something End Function Hopefully this makes sense. I want to be able