1

I have a code that allows file upload and some text input. It uses the uploaded file in an imagacreatefromjepg and imagecopymerge. I want to resize the uploaded file to a definite size which is 255x175. how can i make it? Here is what is have:

        $now = time();
    while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
    {
        $now++;
    }

    // now let's move the file to its final and allocate it with the new filename
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
        or error('receiving directory insuffiecient permission', $uploadForm);

    $upload = $uploadFilename;
    $im = imagecreatefromjpeg("bg.jpg");
    $img2 = imagecreatefromjpeg($upload);
    $black = imagecolorallocate($im, 0, 0, 0);
    $font = 'arialbi.ttf';
    $font2 = 'ariali.ttf';

    imagettftext($im, 24, 0, 50, 280, $black, $font, $title);
    imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);

    imagecopymerge($im, $img2, 30, 350, 0, 0, imagesx($img2), imagesy($img2), 100);

    $date_created = date("YmdHis");//get date created
    $img_name = "-img_entry.jpg"; //the file name of the generated image
    $img_newname = $date_created . $img_name; //datecreated+name
    $img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image 
    imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality

    $newpath = "/home3/site/public_html/mainfolder/image_entry/";//path to another folder
    $newdir = $newpath.$img_newname;
    copy ($img_dir, $newdir); //copy to new folder

    imagedestroy($im);

Hope you can help me fix this. I have raed the posts Resize image before uploading PHP and Php resize width fix. But I dont know how to apply it in my case. Thanks for any and all help.

Community
  • 1
  • 1
Jben Kaye
  • 171
  • 1
  • 5
  • 16
  • why you want to resize before it is uploading? –  Nov 05 '13 at 03:42
  • in my case i have the file already uploaded before i use it in imagecreatefrom jpeg and imagecopymerge. is there a way i can resize it efore saving it to the server? – Jben Kaye Nov 05 '13 at 03:49
  • check here -> http://stackoverflow.com/questions/10333971/html5-pre-resize-images-before-uploading –  Nov 05 '13 at 04:07

1 Answers1

0

just after: imagecopymerge($im,... line

$mini_width = ...;// calculate desirable width
$mini_height = ...;// calculate desirable height
$mini_img = @ImageCreateTrueColor($mini_width, $mini_height);
imagecopyresampled($mini_img, $img2, 0, 0, 0, 0, $mini_width, $mini_height, imagesx($img2), imagesy($img2));

next line is $date_created = date(...

change imagejpeg($im, $img_dir , 80); to:

imagejpeg($mini_img, $img_dir , 80);

and finally change imagedestroy($im); to imagedestroy($mini_img);

Actually, you can, but not have to call imagedestroy() for all the resource images created above.

Ruben Kazumov
  • 3,803
  • 2
  • 26
  • 39
  • Thank you @Ruben for answering. It's the $img2 that i actually want to resize. It's not the background image but the uploaded one i used in here 'imagecopymerge($im, $img2, 30, 350, 0, 0, imagesx($img2), imagesy($img2), 100);'. I have two images in my code. one background and one to place above it. I'm sorry if I confused you. How can i apply your answer with $img2? thanks – Jben Kaye Nov 05 '13 at 03:23
  • Oh! No problem! It's totally my fault! Thank you! It is fixed. – Ruben Kazumov Nov 05 '13 at 03:49
  • thanks again. so how can i call now the resized image so ican exctly use it in `imagecopymerge($im, $img2, 30, 350, 0, 0, imagesx($img2), imagesy($img2), 100);`. in your code, the `imagepng($mini_img);` creates the resized image right? thanks again for firther help :) – Jben Kaye Nov 05 '13 at 03:53
  • Are you ever thinking about eliminate `imagettftext()` because of kerning and outlines? You can just `imagecopymerge()` with transparent high quality png with perfect kerned text block. – Ruben Kazumov Nov 05 '13 at 03:54
  • Actually till the `imagepng()/imagejpeg()' you have `$im` -- resource with background, `$img2` - resource with photo over background and `$mini_img` -- minimized final image. You can do anything you want with them. `imagepng()` just flush resource out to the browser and stops execution. – Ruben Kazumov Nov 05 '13 at 04:01
  • no it's not the final image i want to resize. it's the image over backegorund that i want to resize. (those are the images uploaded by the user)...the final image doesnot need to be displayed in the browser. it's being saved in my sever. i edited my code above for you to see. im really sorry if my question is confusing. thank for further help – Jben Kaye Nov 05 '13 at 05:27