Jump to content

mcfcstock

New Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by mcfcstock

  1. Hello, what's the correct use of GD library to compress JPG images please? I am using this upload script with TinyMCE. See below :

    I have this snippet but it doesn't compress the image:

    		// Accept upload if there was no origin, or if it is an accepted origin
    		// If image is JPEG compress with GD library
    		$filetowrite = $imageFolder . $temp['name'];
    		if (in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("jpg", "jpeg"))) {
    		imagejpeg($temp['tmp_name'], $filetowrite, 75);
    		} else {
    		move_uploaded_file($temp['tmp_name'], $filetowrite);
    		}
    <?php
    	$accepted_origins = array("http://localhost", "http://127.0.0.1", "http://192.168.1.1", "http://example.com");
    
    	$month = strtolower(date('M'));
    	$year = date('Y');
    	$newspath = "img/news/";
    
    	if (!is_dir("$newspath/$year/$month")) {
        mkdir("$newspath/$year/$month", 0777, true);
    }
    	$imageFolder = "$newspath/$year/$month/";
    
    	reset ($_FILES);
    	$temp = current($_FILES);
    	if (is_uploaded_file($temp['tmp_name'])){
    		if (isset($_SERVER['HTTP_ORIGIN'])) {
    			// same-origin requests won't set an origin. If the origin is set, it must be valid.
    			if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
    				header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
    			} else {
    				header("HTTP/1.1 403 Origin Denied");
    				return;
    			}
    		}
    
    		/*
    			If your script needs to receive cookies, set images_upload_credentials : true in
    			the configuration and enable the following two headers.
    		*/
    		// header('Access-Control-Allow-Credentials: true');
    		// header('P3P: CP="There is no P3P policy."');
    
    		// Sanitize input
    		if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
    				header("HTTP/1.1 400 Invalid file name.");
    				return;
    		}
    
    		// Verify extension
    		if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "jpeg", "png"))) {
    				header("HTTP/1.1 400 Invalid extension.");
    				return;
    		}
    
    		// Accept upload if there was no origin, or if it is an accepted origin
    		// If image is JPEG compress with GD library
    		$filetowrite = $imageFolder . $temp['name'];
    		if (in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("jpg", "jpeg"))) {
    		imagejpeg($temp['tmp_name'], $filetowrite, 75);
    		} else {
    		move_uploaded_file($temp['tmp_name'], $filetowrite);
    		}
    		// Respond to the successful upload with JSON.
    		// Use a location key to specify the path to the saved image resource.
    		// { location : '/your/uploaded/image/file'}
    		// echo json_encode(array('location' => $filetowrite));
    
    		$passpath = $year.'/'.$month.'/'.$temp['name'];
    
    		echo json_encode(array('location' => $passpath));
    	} else {
    		// Notify editor that the upload failed
    		header("HTTP/1.1 500 Server Error");
    	}
    ?>

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.