Gå til innhold

Problemer med resizing av bilder


Anbefalte innlegg

Hei, har satt sammen dette.

 

 

 

function resize( $image, $file_type, $path, $image_type)
{

if( $image_type == "personal_picture" )
{
$newwidth = 200; 
$newheight = 300;
}
elseif( $image_type == "avatar" ) {
$newwidth = 80; 
$newheight = 80;
}


if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
	$new_img = imagecreatefromjpeg($image);
elseif( $file_type == "image/x-png" || $file_type == "image/png" ) {
$png = 1;
	$new_img = imagecreatefrompng($image);
	}
elseif($file_type == "image/gif") {
$gif = 1;
	$new_img = imagecreatefromgif($image);
	}





list($width, $height) = getimagesize($image);

if( $width > $newwidth )
{
$img_ratio = $width/$newwidth;
$after_height = $height/$img_ratio;
if( $after_height > $newheight )
{
	$img_ratio = $after_height/$height;
	$after_width = $height/$img_ratio;
	if( $width > $newwidth )
	{
		$img_ratio = $width/$newwidth;
		$after_height = $height/$img_ratio;

	}
}
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

echo '<br>'.$img_ratio;
echo '<br>'.$newheight;
echo '<br>'.$newwidth;
}
elseif( $height > $newheight )
{
$img_ratio = $height/$newheight;
$after_width = $width/$img_ratio;
if( $after_width > $newwidth )
{
	$img_ratio = $after_width/$width;
	$after_height = $width/$img_ratio;
	if( $height > $newheight )
	{
		$img_ratio = $height/$newheight;
		$after_width = $width/$img_ratio;

	}
}
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

}


else
{
$newwidth = $width;
$newheight = $height;
}

if( function_exists( imagecreatetruecolor ) )
	$resized_img = imagecreatetruecolor($newwidth,$newheight);
else
	die("Error: Please make sure you have GD library ver 2+");

imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
ImageJpeg ($resized_img,$path);

$image = $new_img;
$new_img = $resized_img;
/**
* Try to preserve transparency info
* $orig_type - Original Image Type
* $orig_img  - GD Image Resource for original image
* $new_img   - GD Image Resource for resized image
*/

// Transparency only available for GIFs & PNGs
if ($gif || $png ) {
$trnprt_indx = imagecolortransparent($image);

// If we have a specific transparent color
if ($trnprt_indx >= 0) {

	// Get the original image's transparent color's RGB values
	$trnprt_color	= imagecolorsforindex($image, $trnprt_indx);

	// Allocate the same color in the new image resource
	$trnprt_indx	= imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $trnprt_indx);

	// Set the background color for new image to transparent
	imagecolortransparent($new_img, $trnprt_indx);

// Always make a transparent background color for PNGs that don't have one allocated already
} elseif ($png) {

	// Turn off transparency blending (temporarily)
	imagealphablending($new_img, false);

	// Create a new transparent color for image
	$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $color);

	// Restore transparency blending
	imagesavealpha($new_img, true);
}
}


ImageDestroy ($image);
ImageDestroy ($new_img);
}

 

 

 

men det blir bare feil. Feil storrelse pa bildene, og ikke gjennomsiktig.

Endret av Ståle
Lenke til kommentar
Videoannonse
Annonse

mja, det var ogsa noe ja.

 

 

function resize( $image, $file_type, $path, $image_type)
{

if( $image_type == "personal_picture" )
{
$newwidth = 200; 
$newheight = 300;
}
elseif( $image_type == "avatar" ) {
$newwidth = 80; 
$newheight = 80;
}


if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
{
	$jpg = 1;
	$new_img = imagecreatefromjpeg($image);
}
elseif( $file_type == "image/x-png" || $file_type == "image/png" )
{
	$png = 1;
	$new_img = imagecreatefrompng($image);
}
elseif($file_type == "image/gif")
{
	$gif = 1;
	$new_img = imagecreatefromgif($image);
}





list($width, $height) = getimagesize($image);

if( $width > $newwidth )
{
$img_ratio = $width/$newwidth;
$after_height = $height/$img_ratio;
if( $after_height > $newheight )
{
	$img_ratio = $after_height/$height;
	$after_width = $height/$img_ratio;
	if( $width > $newwidth )
	{
		$img_ratio = $width/$newwidth;
		$after_height = $height/$img_ratio;

	}
}
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

echo '<br>'.$img_ratio;
echo '<br>'.$newheight;
echo '<br>'.$newwidth;
}
elseif( $height > $newheight )
{
$img_ratio = $height/$newheight;
$after_width = $width/$img_ratio;
if( $after_width > $newwidth )
{
	$img_ratio = $after_width/$width;
	$after_height = $width/$img_ratio;
	if( $height > $newheight )
	{
		$img_ratio = $height/$newheight;
		$after_width = $width/$img_ratio;

	}
}
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

}


else
{
$newwidth = $width;
$newheight = $height;
}

if( function_exists( imagecreatetruecolor ) )
	$resized_img = imagecreatetruecolor($newwidth,$newheight);
else
	die("Error: Please make sure you have GD library ver 2+");

imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

$image = $new_img;
$new_img = $resized_img;
/**
* Try to preserve transparency info
* $orig_type - Original Image Type
* $orig_img  - GD Image Resource for original image
* $new_img   - GD Image Resource for resized image
*/

// Transparency only available for GIFs & PNGs
if ($gif || $png ) {
$trnprt_indx = imagecolortransparent($image);

// If we have a specific transparent color
if ($trnprt_indx >= 0) {

	// Get the original image's transparent color's RGB values
	$trnprt_color	= imagecolorsforindex($image, $trnprt_indx);

	// Allocate the same color in the new image resource
	$trnprt_indx	= imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $trnprt_indx);

	// Set the background color for new image to transparent
	imagecolortransparent($new_img, $trnprt_indx);

// Always make a transparent background color for PNGs that don't have one allocated already
} elseif ($png) {

	// Turn off transparency blending (temporarily)
	imagealphablending($new_img, false);

	// Create a new transparent color for image
	$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $color);

	// Restore transparency blending
	imagesavealpha($new_img, true);
}
}

if( $jpg )
ImageJpeg ($new_image,$path);
if( $gif)
Imagegif ($new_image,$path);
if( $png )
Imagepng ($new_image,$path);

ImageDestroy ($image);
ImageDestroy ($new_img);
}

 

 

men na far jeg feil

 

Warning: imagepng(): supplied argument is not a valid Image resource in resize.php on line 630

Lenke til kommentar

Har endret en del pa det na

 

 

function resize( $image, $file_type, $path, $image_type)
{

if( $image_type == "personal_picture" )
{
$newwidth = 200; 
$newheight = 300;
}
elseif( $image_type == "avatar" ) {
$newwidth = 80; 
$newheight = 80;
}


if($file_type == "image/pjpeg" || $file_type == "image/jpeg" || $file_type == "application/octet-stream")
{
	$jpg = 1;
	$new_img = imagecreatefromjpeg($image);
}
elseif( $file_type == "image/x-png" || $file_type == "image/png" )
{
	$png = 1;
	$new_img = imagecreatefrompng($image);
}
elseif($file_type == "image/gif")
{
	$gif = 1;
	$new_img = imagecreatefromgif($image);
}





list($width, $height) = getimagesize($image);


if( $width > $newwidth )
{
$img_ratio = $width/$newwidth;
$after_height = $height/$img_ratio;

echo $newwidth;
echo '<br>';
echo $newheight;
echo '<br>';
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

echo $newwidth;
echo '<br>';
echo $newheight;
echo '<br>';
}
/*if( $after_height > $newheight )
{
	$img_ratio = $after_height/$height;
	$after_width = $height/$img_ratio;
	if( $width > $newwidth )
	{
		$img_ratio = $width/$newwidth;
		$after_height = $height/$img_ratio;

	}
}

echo '<br>'.$img_ratio;
echo '<br>'.$newheight;
echo '<br>'.$newwidth;
}
elseif( $height > $newheight )
{
$img_ratio = $height/$newheight;
$after_width = $width/$img_ratio;
if( $after_width > $newwidth )
{
	$img_ratio = $after_width/$width;
	$after_height = $width/$img_ratio;
	if( $height > $newheight )
	{
		$img_ratio = $height/$newheight;
		$after_width = $width/$img_ratio;

	}
}
$newheight = (isset( $after_height ) ) ? $after_height : $newheight;
$newwidth = (isset( $after_width ) ) ? $after_width : $newwidth;

}
*/


if( function_exists( imagecreatetruecolor ) )
	$resized_img = imagecreatetruecolor($newwidth,$newheight);
else
	die("Error: Please make sure you have GD library ver 2+");

imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

$image = $new_img;
$new_img = $resized_img;
/**
* Try to preserve transparency info
* $orig_type - Original Image Type
* $orig_img  - GD Image Resource for original image
* $new_img   - GD Image Resource for resized image
*/

// Transparency only available for GIFs & PNGs
if ($gif || $png ) {
$trnprt_indx = imagecolortransparent($image);

// If we have a specific transparent color
if ($trnprt_indx >= 0) {

	// Get the original image's transparent color's RGB values
	$trnprt_color	= imagecolorsforindex($image, $trnprt_indx);

	// Allocate the same color in the new image resource
	$trnprt_indx	= imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $trnprt_indx);

	// Set the background color for new image to transparent
	imagecolortransparent($new_img, $trnprt_indx);

// Always make a transparent background color for PNGs that don't have one allocated already
} elseif ($png) {

	// Turn off transparency blending (temporarily)
	imagealphablending($new_img, false);

	// Create a new transparent color for image
	$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

	// Completely fill the background of the new image with allocated color.
	imagefill($new_img, 0, 0, $color);

	// Restore transparency blending
	imagesavealpha($new_img, true);
}
}

/*
if( $jpg )
ImageJpeg ($new_image,$path, 100);
if( $gif)
Imagegif ($new_image,$path);
if( $png )
Imagepng ($new_image,$path);
ImageDestroy ($image);
ImageDestroy ($new_img);
*/
}

 

 

 

Outputten viser at det er riktig bredde og hoyde

80

80

80

137.049180328

 

men na blir ikke bildet resized i det hele tatt. :s

Lenke til kommentar

Er nok ikke det.

 

Jeg fikk det faktisk til na :)

 

 

function resize( $orig_image, $file_type, $path, $image_type)
{

if( $image_type == "personal_picture" )
{
$pre_width = 200; 
$pre_height = 300;
}
elseif( $image_type == "avatar" ) {
$pre_width = 80; 
$pre_height = 80;
}


if($file_type == "image/pjpeg" || $file_type == "image/jpeg" || $file_type == "application/octet-stream")
{
	$jpg = 1;
	$image = imagecreatefromjpeg($orig_image);
}
elseif( $file_type == "image/x-png" || $file_type == "image/png" )
{
	$png = 1;
	$image = imagecreatefrompng($orig_image);
}
elseif($file_type == "image/gif")
{
	$gif = 1;
	$image = imagecreatefromgif($orig_image);
}





list($width, $height) = getimagesize($orig_image);


if( $width > $pre_width || $height > $pre_height ) {

if( $width > $pre_width )
{
$img_ratio = $width/$pre_width;
$after_height = ceil( $height/$img_ratio);

$newheight = (isset( $after_height ) ) ? $after_height : $pre_height;
$newwidth = (isset( $after_width ) ) ? $after_width : $pre_width;


if( $newheight > $pre_height )
{
$img_ratio = $newheight/$pre_height;
$after_width = ceil( $newwidth/$img_ratio );
$after_height = ceil( $newheight/$img_ratio );

$newheight = (isset( $after_height ) ) ? $after_height : $pre_height;
$newwidth = (isset( $after_width ) ) ? $after_width : $pre_width;

}

}


elseif( $height > $pre_height && !isset( $newheight ) )
{
$img_ratio = $height/$pre_height;
$after_width = $width/$img_ratio;


$newheight = (isset( $after_height ) ) ? $after_height : $pre_height;
$newwidth = (isset( $after_width ) ) ? $after_width : $pre_width;

if( $newwidth > $pre_width )
{
$img_ratio = $newwidth/$pre_width;
$after_height = $newheight/$img_ratio;
$after_width = $newwidth/$img_ratio;

$newheight = (isset( $after_height ) ) ? $after_height : $pre_height;
$newwidth = (isset( $after_width ) ) ? $after_width : $pre_width;

}

}





if( function_exists( imagecreatetruecolor ) )
	$resized_img = imagecreatetruecolor($newwidth,$newheight);
else
	die("Error: Please make sure you have GD library ver 2+");

/**
* Try to preserve transparency info
* $orig_type - Original Image Type
* $orig_img  - GD Image Resource for original image
* $new_img   - GD Image Resource for resized image
*/

// Transparency only available for GIFs & PNGs
if ($gif || $png ) {
$trnprt_indx = imagecolortransparent($image);

// If we have a specific transparent color
if ($trnprt_indx >= 0) {

	// Get the original image's transparent color's RGB values
	$trnprt_color	= imagecolorsforindex($image, $trnprt_indx);

	// Allocate the same color in the new image resource
	$trnprt_indx	= imagecolorallocate($resized_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

	// Completely fill the background of the new image with allocated color.
	imagefill($resized_img, 0, 0, $trnprt_indx);

	// Set the background color for new image to transparent
	imagecolortransparent($resized_img, $trnprt_indx);

// Always make a transparent background color for PNGs that don't have one allocated already
} elseif ($png) {

	// Turn off transparency blending (temporarily)
	imagealphablending($resized_img, false);

	// Create a new transparent color for image
	$color = imagecolorallocatealpha($resized_img, 0, 0, 0, 127);

	// Completely fill the background of the new image with allocated color.
	imagefill($resized_img, 0, 0, $color);

	// Restore transparency blending
	imagesavealpha($resized_img, true);
}
}

imagecopyresampled($resized_img, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

if( $jpg )
ImageJpeg ($resized_img,$path, 100);
if( $gif)
Imagegif ($resized_img,$path);
if( $png )
Imagepng ($resized_img,$path);
ImageDestroy ($image);
ImageDestroy ($resized_img);

}
}

 

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...