0

i am new to php. i am trying to upload an image, resize it, and then display it without saving. i am using gd to do this. the codes provided here is just a basic approach for just the function to work.

<?php
if (!isset($_FILES['image']['tmp_name'])) {
}   
else{
$file=$_FILES['image']['tmp_name'];
$img_src = imagecreatefromstring(file_get_contents($file));
$img_dest = imagecreatetruecolor(851, 315);
$src_width = imagesx($img_src);
$src_height = imagesy($img_src);
imagecopyresized($img_dest, $img_src, 0, 0, 0, 0, 851, 315, $src_width, $src_height);
$text= $_POST['text'];
$font_path = 'arial.TTF';
$grey = imagecolorallocate($img_dest, 128, 128, 128);
$black = imagecolorallocate($img_dest, 0, 0, 0);
imagettftext($img_dest, 25, 0, 302, 62, $grey, $font_path, $text);
imagettftext($img_dest, 25, 0, 300, 60, $black, $font_path, $text);
header( "Content-type: image/png" );
imagepng( $img_dest );
imagedestroy( $img_dest );
imagedestroy( $img_src );
}
?>

i am uploading an image through a form and running this script. the image is being displayed. but how can i display more than 1 images in different size using this method. with regards.

  • Hi, there are so many examples of this online, it's not a good fit for a Stack Overflow question. Try a search for `php resize image` and similar. The manual for these functions has examples as well. – Pekka Jul 12 '13 at 07:44
  • e.g. http://php.net/manual/en/function.imagecopyresampled.php – Pekka Jul 12 '13 at 07:45

2 Answers2

0

I found a solution in a old question for changing the size of a image

$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
$original_img = imagecreatefromjpg($filename);
$thumb_w = 100;
$thumb_h = 100;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($thumb_img, $original_img,
                   0, 0,
                   0, 0,
                   $original_w, $original_h
                   $thumb_w, $thumb_h);
imagejpeg($thumb_img, $thumb_filename);
destroyimage($thumb_img);
destroyimage($original_img);

Change Image Size - PHP

Community
  • 1
  • 1
0

You should create two images. One you can create directly from the source

$img_src = imagecreatefrompng($file);

or

$img_src = imagecreatefromjpeg($file);

or

$img_src = imagecreatefromstring(file_get_contents($file));

getting the file size of the src file:

$sizes = imagesize($img_src);
$src_width = $sizes[0];
$src_height = $sizes[1];

But now the image will be scaled to 200x200 even if the src image does not have been same with and height. You can prevent this by calculating the dst-size:

$faktor = ($src_width > $src_height ? $src_width : $src_height);
$faktor = 100 / $faktor;

$f_width = round($src_width * $faktor);
$f_height = round($src_height * $faktor);

$new_w = 200 * $f_width;
$new_h = 200 * $f_height;

The second one you can create from with your destination size

$img_dest = imagecreatetruecolor($new_w, $new_h);

And then you may copy the resized source to new one

imagecopyresized($img_dest, $img_src, 0, 0, 0, 0, $new_w, $new_h, $src_width, $src_height);
header( "Content-type: image/png" );
imagepng( $img_dest );
imagedestroy( $img_dest );
imagedestroy( $img_src );

P.S.: When creating image from string i think its not good to addslashes to the contents.

DaKirsche
  • 352
  • 1
  • 14
  • still getting broken image... :( – Santanu Kumar Jul 12 '13 at 08:01
  • does $src_width and $src_height needs to be mentioned manually. – Santanu Kumar Jul 12 '13 at 08:06
  • So whats your code right now? Try to comment out the header and echo the nessecary data and check wether you have correct content for sizes and image. or try first to make `imagepng( $img_src );` instead of the `imagepng( $img_dest );` to check if the src image is correct before resizing. – DaKirsche Jul 12 '13 at 08:08
  • you can get the size by `$s = getimagesize($img_src);` with $s[0] as width and $s[1] as height – DaKirsche Jul 12 '13 at 08:10
  • thanks. i got it working. but the image is getting croped. please tell me how to get the full image in new size. please take a look at the codes i have edited(main question) – Santanu Kumar Jul 12 '13 at 08:23
  • So now you define that the src-image ist just 100x100 pixels in size and you resize iut to 200x200. so if the image src is like 300x300 you will just take the upper left corner with 100x100 an resize it. – DaKirsche Jul 12 '13 at 08:31
  • the image is being displayed but not being saved through right click method from the window. whenever i try to save the image, it getting saved as .php how can i make it save as png. (codes edited) – Santanu Kumar Jul 12 '13 at 08:45
  • Normally it should be saved as png right now. have you tried to rename the downloaded file to .png or do you configured your system to hide known file extensions? – DaKirsche Jul 12 '13 at 08:52
  • I'm glad that it helps. Please mark answer as useful to show the problem is solved. – DaKirsche Jul 12 '13 at 08:59
  • unfortunately i dont have reputation points right now. will surely vote this up as soon as i get to 15 reputations points. – Santanu Kumar Jul 12 '13 at 09:07