# picture resize



## kaits (4. April 2003)

How can I here resize the picture to maximum 430 of width? And the quality shoult be good.
I know, that I must use the imageCopyResampled() function, but I don't know how to insert it into my code to resize the pictuse 

Some examples would be useful.
Thanks 



```
<?
if(stristr($string,'[ IMG]')&&stristr($string,'[ /IMG]'))
{
	$string=preg_replace("'\[ img\](\S+?[\w\d]{1}\.(gif|jpg|jpeg|png))\[ /IMG\]'iUs",'<a href="\\1" target="_blank"><img src="\\1" border="0"></a>',$string);
	$string=preg_replace("'\[ [/]{0,1}img\]'iUs",'',$string);
}
?>
```


----------



## Sir Robin (5. April 2003)

Hi...if you want to resize it to an maximum of width you can use the imagecopyresized-function of the GD-Library..it works only with jpeg..but it´s easy to adapt it for other formats

I have an example funktion for you, which resizes an image relativly...


```
<?php

function resize_image($image,$max_width,$max_height){

//First of all..grab some image infos
$picinfos=getimagesize($image);

//Create an new image from the old one
$oldpic=ImageCreateFromJPEG($image);

//Check which site is bigger, and calculate the factor to resize
if($picinfos[0]>$picinfos[1]){
   $fator=$max_width / $picinfos[0];
   }else{
    $factor=$max_height / $picinfos[1];
    }

//calculate new sizes
$new_width=$picinfos[0] * $factor;
$new_height=$picinfos[1] * $factor;

//Create an new blank image with new sizes
$newpic=imagecreate($new_width,$new_height);

//Copy resized old pic into new pic
ImageCopyResized($newpic,$oldpic,0,0,0,0,$new_width,$new_height,$picinfos[0],$picinfos[1]);

//now you can save it, with ImageJPEG($newpic,"directory/to/save/in");

}

?>
```

i hope the grammar and the language in the comments and so are right...cause it´s to damn early this day


----------



## Fabian H (5. April 2003)

You forgott the _function_ instruction.


----------



## Sir Robin (5. April 2003)

damn...I´ve said it´s early today 

thx @ Nuinmondo


----------



## kaits (5. April 2003)

theis line: $factor=$max_height / $picinfos[1];
gives me this error: Division by zero


----------



## SilentWarrior (5. April 2003)

that's strange. that would mean the pic has a width of 0px. but try this:
	
	
	



```
if($picinfos[0] == 0)
  $picinfos[0] = 1;
if($picinfos[1] == 0)
  $picinfos[1] == 1;
```


----------



## Sir Robin (5. April 2003)

ehm...I´ve tested it, and I haven´t any problems...

you must call the function like this:


```
<?php

//...

resize_image("path/to/picture.jpg",100,100);

?>
```

...there must be your error...or your image has an height of 0 .. but this seems to be very unbelievable..


----------



## heiko-san (30. November 2010)

Hi,

I know this thread is rather old, but right now I'm doing exactly what this "tutorial" tells. 
However, my picture is resized as expected. But the quality afterwards is really bad. my pictures are jpg type at a resolution of 1024x768 and I'm resizing them to 800x600. If I'm doing this manually using a graphics program the result isn't nearly that pixelized, so I was wondering if there is another, better solution for automatical resize with php scripts?


----------

